Spring自动装配总结
1、 只要设置需要自动装配的<bean>中的autowire属性即可让Spring自动装配。
2、 自动装配的四种类型:
(1) byName:在容器中寻找和需要自动装配的属性名相同的Bean;如没有找到相符的Bean,该属性就没有被装配上,
(2) byType:在容器中寻找一个与需要自动装配的属性类型相同的Bean;如果没有找到相符的Bean,该属性就没有被装配上,如找到超过一个相符的Bean抛出异常org.springframework.beans.factory.UnsatisfiedDependencyException。
(3) constructor:在容器中查找与需要自动装配的Bean的构造方法参数一致的一个或多个Bean。如存在不确定Bean或构造方法,容器会抛出异常org.springframework.beans.factory.UnsatisfiedDependencyException。
(4) autodetect:首先尝试使用contructor来自动装配,然后使用byType方式。不确定性的处理与constructor和byType方式一样。
3、 四种类型的使用说明及分析代码:
(1) 使用byName时候的配置文件beanByName.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- autowire自动装配bean的配置属性
byName根据名称自动装配
cn.csdn.hr.service.CustomerServiceImpl属性名称与bean id的名称一致的时候就自动装配
-->
<bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="byName"/>
<bean id="customerDao" class="cn.csdn.hr.dao.CustomerDaoImpl"/>
<bean id="baseDao" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>
</beans>
(2) 使用byType时候的配置文件beanByType.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- autowire自动装配bean的配置属性
byType根据类型自动装配
cn.csdn.hr.service.CustomerServiceImpl属性类型与bean中有相同类型的时候就自动装配
-->
<bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="byType"/>
<bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/>
<bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>
<!-- 类型冲突 -->
<bean id="customerDao" class="cn.csdn.hr.dao.CustomerDaoImpl"/>
</beans>
注意:当类型冲突时,会抛出异常
(3)使用constructor时候的配置文件beanconstructor.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- autowire自动装配bean的配置属性
constructor根据类型自动装配 是构造器的参数
cn.csdn.hr.service.CustomerServiceImpl构造器的参数类型与bean中有相同类型的时候就自动装配
-->
<bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="constructor"/>
<bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/>
<bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>
<!-- 类型冲突 -->
<bean id="customerDao" class="cn.csdn.hr.dao.CustomerDaoImpl"/>
</beans>
(4)使用autodetect时候的配置文件beanautodetect.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- autowire自动装配bean的配置属性
autodetect看下面这个类
cn.csdn.hr.service.CustomerServiceImpl有没有默认的构造器 有默认的构造器,就采用byType。如果没有则采用constructor。
-->
<bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="autodetect"/>
<bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/>
<bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>
</beans>