相思相见知何日?此时此夜难为情。java
在Spring
框架中,在配置文件中声明bean
的依赖关系是一个很好的作法,由于Spring
容器可以自动装配协做bean
之间的关系。这称为spring
自动装配。git
自动装配功能具备四种模式。分别是 no
,byName
,byType
和constructor
。github
已弃用另外一种自动连线模式自动检测。
Docs
说autodetect
选项提供了太多的magic
,最好使用更明确的声明。spring
XML
配置中的默认自动装配模式为no
。Java
配置中的默认自动装配模式是byType
。no
该选项是spring
框架的默认选项,表示自动装配为关闭状态OFF
。咱们必须在bean
定义中使用<property>
标签显式设置依赖项。byName
此选项启用基于bean
名称的依赖项注入。在Bean
中自动装配属性时,属性名称用于在配置文件中搜索匹配的Bean
定义。若是找到这样的bean
,则将其注入属性。若是找不到这样的bean
,则会引起错误。byType
此选项支持基于bean
类型的依赖项注入。在bean
中自动装配属性时,属性的类类型用于在配置文件中搜索匹配的bean
定义。若是找到这样的bean
,就在属性中注入它。若是没有找到这样的bean
,就会引起一个错误。constructor
经过构造函数自动装配与byType
类似,仅适用于构造函数参数。在启用了自动装配的bean
中,它将查找构造函数参数的类类型,而后对全部构造函数参数执行自动装配类型。请注意,若是容器中没有一个彻底属于构造函数参数类型的bean
,则会引起致命错误。@Autowired
注解除了bean
配置文件中提供的自动装配模式以外,还可使用@Autowired
注解在bean
类中指定自动装配。要在bean
类中使用@Autowired
自动注入,必须首先使用如下配置在spring
应用程序中启用自动注入。安全
<context:annotation-config />
使用配置文件中的AutowiredAnnotationBeanPostProcessor
bean
定义能够实现相同的目的。微信
<bean class ="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
@Autowired
注解如今,启用注解配置后,能够随意使用@Autowired
自动链接bean
依赖项。这能够经过三种方式完成:框架
@Autowired
属性在属性上使用@Autowired
时,等效于在配置文件中经过byType
自动注入函数
public class EmployeeBean { @Autowired private DepartmentBean departmentBean; public DepartmentBean getDepartmentBean() { return departmentBean; } public void setDepartmentBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } //More code }
@Autowired
在属性setter
方法上在属性的setter
方法上使用@Autowired
时,它也等效于在配置文件中经过byType
进行自动装配。ui
public class EmployeeBean { private DepartmentBean departmentBean; public DepartmentBean getDepartmentBean() { return departmentBean; } @Autowired public void setDepartmentBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } //More code }
@Autowired
在构造函数上在bean
的构造函数上使用@Autowired
时,它也等同于在配置文件中经过 constructor
进行自动装配。this
package cn.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 }
@Qualifier
解决冲突咱们了解到,若是咱们在byType
模式下使用自动装配,容器会在属性类类型中查找依赖项。若是找不到这样的类型,则会引起错误。可是,若是有两个或多个相同类类型的bean
,该怎么办?
在这种状况下,spring
将没法选择正确的bean
来注入属性,所以你将须要使用@Qualifier
注解来帮助容器。
要解析特定的bean
,咱们须要使用@Qualifier
注解以及@Autowired
注解,并将bean
名称传递到注解参数中。看看下面的例子:
public class EmployeeBean{ @Autowired @Qualifier("finance") private DepartmentBean departmentBean; public DepartmentBean getDepartmentBean() { return departmentBean; } public void setDepartmentBean(DepartmentBean departmentBean) { this.departmentBean = departmentBean; } //More code }
其中重复的bean
配置以下:
<?xml version="1.0" encoding="UTF-8"?> <beans> <context:annotation-config /> <bean id="employee" class="cn.howtodoinjava.autowire.constructor.EmployeeBean" autowire="constructor"> <property name="fullName" value="Lokesh Gupta"/> </bean> <!--First bean of type DepartmentBean--> <bean id="humanResource" class="cn.howtodoinjava.autowire.constructor.DepartmentBean" > <property name="name" value="Human Resource" /> </bean> <!--Second bean of type DepartmentBean--> <bean id="finance" class="cn.howtodoinjava.autowire.constructor.DepartmentBean" > <property name="name" value="Finance" /> </bean> </beans>
required = false
进行错误安全的自动装配即便在自动装配Bean
依赖项时已格外当心,仍然可能会发现奇怪的查找失败。所以,要解决此问题,您将须要使自动装配成为可选的,以便在未找到依赖项的状况下,应用程序不该引起任何异常,而自动装配应被忽略。
这能够经过两种方式完成:
bean
属性的非强制性的特定bean
自动装配,能够在@Autowired
注解中使用required =“ false”
属性。 @Autowired (required=false) @Qualifier ("finance") private DepartmentBean departmentBean;`
bean
中的全部属性)应用可选的自动装配;使用如下配置设置。 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"> <property name="requiredParameterValue" value="false" /> </bean>
默认状况下,自动装配扫描并匹配范围内的全部bean
定义。若是您想排除一些bean
定义,这样它们就不能经过自动装配模式被注入,可使用设置为false
的autowire-candidate
来作到这一点。
autowire-candidate
做为false
彻底将bean
排除在自动装配候选以外。它将特定的bean
定义彻底排除在自动装配基础结构以外。 <?xml version="1.0" encoding="UTF-8"?> <beans> <context:annotation-config /> <bean id="employee" class="cn.howtodoinjava.autowire.constructor.EmployeeBean" autowire="constructor"> <property name="fullName" value="Lokesh Gupta"/> </bean> <!--Will be available for autowiring--> <bean id="humanResource" class="cn.howtodoinjava.autowire.constructor.DepartmentBean" > <property name="name" value="Human Resource" /> </bean> <!--Will not participate in autowiring--> <bean id="finance" class="cn.howtodoinjava.autowire.constructor.DepartmentBean" autowire-candidate="false"> <property name="name" value="Finance" /> </bean> </beans>
bean
名称的模式匹配来限制自动装配候选对象。顶级<beans/>
元素在其default-autowire-candidate
属性中接受一个或多个属性。 例如,要将自动装配候选状态限制为名称以Impl
结尾的任何bean
,请提供值* Impl
。要提供多种模式,请在以逗号分隔的列表中定义它们。 <?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>
请注意,bean
定义的autowire-candidate
属性的值true
或false
始终优先,而对于此类bean
,模式匹配规则将不适用。
这就是Spring
bean
自动装配的所有内容。
🙂🙂🙂关注微信公众号java干货 不按期分享干货资料