在spring框架中,学习在配置文件中设置bean的依赖是很好的实践,但是spring容器也可以利用bean间的关系实现自动注入,这意味着可以让spring自动通过检测Beanfactory来决定为你的bean输入其他的bean。自动装配可以指定一些类bean并且启用他们,而另一些beans不会被自动装配。
自动装配的模式:
1、NO:一点也没有自动装配,bean的引用关系引用元素定义
2、byName:通过属性的名字自动装配,这种方式将会查找需要被注入的bean的准确的名字
3、byType:允许在容器中类型完全一致的属性注入,如果没有的话,将会报出致命异常
4、constructor:这种方式和byType类似,但是使用于有构造参数的情况
5、autudetect:通过检测bean来决定选择constructor还是使用byType
自动注入功能有5种,分别是NO、byName、byType、constructor和autudetect,默认情况下是NO这种情况,也就是自动装配时关闭的。
<context:annotation-config />
<bean class ="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
public class EmployeeBean { @Autowired private DepartmentBean departmentBean; public DepartmentBean getDepartmentBean() { return departmentBean; } public void setDepartmentBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } //More code }
public class EmployeeBean { private DepartmentBean departmentBean; public DepartmentBean getDepartmentBean() { return departmentBean; } @Autowired public void setDepartmentBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } //More code }
package com.howtodoinjava.autowire.constructor; public class EmployeeBean { @Autowired public EmployeeBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } private DepartmentBean departmentBean; public DepartmentBean getDepartmentBean() { return departmentBean; } public void setDepartmentBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } //More code }
public class EmployeeBean { @Autowired @Qualifier("finance") private DepartmentBean departmentBean; public DepartmentBean getDepartmentBean() { return departmentBean; } public void setDepartmentBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } //More code }
<?xml version="1.0" encoding="UTF-8"?> <beans> <context:annotation-config /> <bean id="employee" class="com.howtodoinjava.autowire.constructor.EmployeeBean" autowire="constructor"> <property name="fullName" value="Lokesh Gupta"/> </bean> <!--First bean of type DepartmentBean--> <bean id="humanResource" class="com.howtodoinjava.autowire.constructor.DepartmentBean" > <property name="name" value="Human Resource" /> </bean> <!--Second bean of type DepartmentBean--> <bean id="finance" class="com.howtodoinjava.autowire.constructor.DepartmentBean" > <property name="name" value="Finance" /> </bean> </beans>
@Autowired (required=false) @Qualifier ("finance") private DepartmentBean departmentBean;
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"> <property name="requiredParameterValue" value="false" /> </bean>
<?xml version="1.0" encoding="UTF-8"?> <beans> <context:annotation-config /> <bean id="employee" class="com.howtodoinjava.autowire.constructor.EmployeeBean" autowire="constructor"> <property name="fullName" value="Lokesh Gupta"/> </bean> <!--Will be available for autowiring--> <bean id="humanResource" class="com.howtodoinjava.autowire.constructor.DepartmentBean" > <property name="name" value="Human Resource" /> </bean> <!--Will not participate in autowiring--> <bean id="finance" class="com.howtodoinjava.autowire.constructor.DepartmentBean" autowire-candidate="false"> <property name="name" value="Finance" /> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire-candidates="*Impl,*Dao"> <context:annotation-config /> <bean id="employee" class="com.howtodoinjava.autowire.constructor.EmployeeBean" autowire="constructor"> <property name="fullName" value="Lokesh Gupta"/> </bean> <!--Will be available for autowiring--> <bean id="humanResource" class="com.howtodoinjava.autowire.constructor.DepartmentBean" > <property name="name" value="Human Resource" /> </bean> <!--Will not participate in autowiring--> <bean id="finance" class="com.howtodoinjava.autowire.constructor.DepartmentBean" autowire-candidate="false"> <property name="name" value="Finance" /> </bean> </beans>