1、什么是:Spring(自动注入)?
自动注入就是:在Spring配置文件中(配置)<property />时, 若(对象名)和(ref="id")的 (id名)相同时, 可以不用配置<property /> , Spring会自动注入。
2、怎么实现:Spring(自动注入)?
Ⅰ、两种配置方式:
㈠、xsd全局配置(关键字) default-autowire=""
㈡、bean中(局部配置)
1、在需要省略的:<bean>标签中加上 <bean autowire="">关键字,然后选择(方式即可)
2、autowire方式:
①、default:根据xsd中的默认值(default-autowire="no")代表(不自动注入)
②、no:代表(不自动注入) (比如) : 当配置<property />,尽管(对象名 == ref(id)), 一旦省略则为(null)
③、byName:通过id(类型注入) (比如) :<property name="dataSource" ref="dataSource"></property> 这样的,name存在ref中,即可省略。
④、byType:通过(类型注入) (比如): 要配置:<bean id="people" class="全限定路径">, class ="全限定路径"的该类中有(属性people):此时Spring可以帮助配置。
⑤、construct:通过(构造器注入 )(比如):若已配置了Teacher (且) People类中有Teacher类(对象)(还有)其(构造方法)则此时(People中的teacher可以省略配置)。 构造器底层为byName
3、注意:
倘若不写:autowire(默认走xsd中设置的: default-autowire)
3、Spring(自动注入)有什么:好处?
让程序员更轻松了, 不用重复造轮子。