Spring3.2.2_自动装配分类:?Spring课程总结2013-04-25 16:33?157人阅读?评论(0)?收藏?举报Spring_Autowiri
Spring3.2.2_自动装配
分类:?Spring课程总结2013-04-25 16:33?157人阅读?评论(0)?收藏?举报
Spring_Autowiring?collaborators
在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配)、byName,byType,constructor下面来分别介绍一下这些是如何自动装配的
???<bean?id="foo"?style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 693px; overflow: auto; padding-top: 1px; color: #362e2b; margin: 18px 0px !important;">[java]?view plaincopy?
- package?www.csdn.spring.autowire.bean;??
- ??
- public?class?CumputerBean?{??
- ??
- //?电脑名称??
- ??
- private?String?name;??
- ??
- public?void?setName(String?name)?{??
- ??
- this.name?=?name;??
- ??
- }??
- ??
- }??
2、创建DeptBean?类
[java]?view plaincopy?
- package?www.csdn.spring.autowire.bean;??
- ??
- ??
- public?class?DeptBean?{??
- ??
- //部门名称??
- ??
- private?String?name;??
- ??
- public?void?setName(String?name)?{??
- ??
- this.name?=?name;??
- ??
- }??
- ??
- }??
3、创建EmployeeBean
[java]?view plaincopy?
- package?www.csdn.spring.autowire.bean;??
- ??
- ??
- public?class?EmployeeBean?{??
- ??
- private?DeptBean?deptBean;??
- ??
- private?CumputerBean?cumputerBean;??
- ??
- ??
- public?void?setDeptBean(DeptBean?deptBean)?{??
- ??
- this.deptBean?=?deptBean;??
- ??
- }??
- ??
- public?void?setCumputerBean(CumputerBean?cumputerBean)?{??
- ??
- this.cumputerBean?=?cumputerBean;??
- ??
- }??
- ??
- @Override??
- ??
- public?String?toString()?{??
- ??
- return?"EmployeeBean?[deptBean="?+?deptBean?+?",?cumputerBean="??
- ??
- +?cumputerBean?+?"]";??
- ??
- }??
- ??
- ??
- }??
首先分析no、byName、byType的配置都是采用setter方法依赖注入实现的案例
1、no配置(通过ref=””引用需要的bean)
[java]?view plaincopy?
- <?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.xsd">??
- ??
- <!--?电脑bean?-->??
- <bean?id="cumputerBean"?class="www.csdn.spring.autowire.bean.CumputerBean">??
- <property?name="name"?value="HP6325笔记本"?/>??
- </bean>??
- ??
- <!--?部门bean?-->??
- <bean?id="deptBean"?class="www.csdn.spring.autowire.bean.DeptBean">??
- <property?name="name"?value="CSDN教育事业部"?/>??
- </bean>??
- ??
- <!--?员工bean??根据EmployeeBean中的属性名称通过ref="bean"去匹配-->??
- <bean?id="employeeBean"?class="www.csdn.spring.autowire.bean.EmployeeBean">??
- <property?name="cumputerBean"?ref="cumputerBean"?/>??
- <property?name="deptBean"?ref="deptBean"?/>??
- ??
- </bean>??
- ??
- </beans>??
2、byName配置(分析:会根据EmployeeBean中属性的名称?自动装配)
[java]?view plaincopy?
- <?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.xsd">??
- ??
- ??
- ??
- <!--?电脑bean?-->??
- ??
- <bean?id="cumputerBean"?class="www.csdn.spring.autowire.bean.CumputerBean">??
- ??
- <property?name="name"?value="HP6325笔记本"?/>??
- ??
- </bean>??
- ??
- <!--?部门bean?-->??
- ??
- <bean?id="deptBean"?class="www.csdn.spring.autowire.bean.DeptBean">??
- ??
- <property?name="name"?value="CSDN教育事业部"?/>??
- ??
- </bean>??
- ??
- <!--?员工bean-->??
- ??
- <bean?id="employeeBean"?class="www.csdn.spring.autowire.bean.EmployeeBean"?autowire="byName"/>??
- ??
- </beans>??
[java]?view plaincopy?
- ???
3、byType配置(分析:会根据EmployeeBean中属性的类型?自动装配)
[java]?view plaincopy?
- <?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.xsd">??
- ??
- <!--?电脑bean?-->??
- ??
- <bean?id="cumputerBean"?class="www.csdn.spring.autowire.bean.CumputerBean">??
- ??
- <property?name="name"?value="HP6325笔记本"?/>??
- ??
- </bean>??
- ??
- <!--?部门bean?-->??
- ??
- <bean?id="deptBean"?class="www.csdn.spring.autowire.bean.DeptBean">??
- ??
- <property?name="name"?value="CSDN教育事业部"?/>??
- ??
- </bean>??
- ??
- <!--?员工bean??根据EmployeeBean中的属性根据类型匹配-->??
- ??
- <bean?id="employeeBean"?class="www.csdn.spring.autowire.bean.EmployeeBean"?autowire="byType"/>??
- ??
- ??
- </beans>??
注意:当根据byType类型装配时,当在容器内找到多个匹配的类型时会出现如下bug
org.springframework.beans.factory.UnsatisfiedDependencyException:?
Error?creating?bean?with?name?'employeeBean'?defined?in?class?path?resource?[spring-byType.xml]:?Unsatisfied?dependency?expressed?through?bean?property?'deptBean':?:
?No?qualifying?bean?of?type?[www.csdn.spring.autowire.bean.DeptBean]?is?defined:?expected?single?matching?bean?but?found?2:?deptBean,deptBean1;?
nested?exception?is?org.springframework.beans.factory.NoUniqueBeanDefinitionException:?
No?qualifying?bean?of?type?[www.csdn.spring.autowire.bean.DeptBean]?is?defined:?expected?single?matching?bean?but?found?2:?deptBean,deptBean1
?
4、Constructor(构造器参数根据byType类型匹配,自动装配)
首先修改EmployeeBean类?修改后代码如下:
[java]?view plaincopy?
- package?www.csdn.spring.autowire.bean;??
- ??
- ??
- public?class?EmployeeBean?{??
- ??
- private?DeptBean?deptBean;??
- private?CumputerBean?cumputerBean;??
- ??
- ??
- public?EmployeeBean(DeptBean?deptBean,?CumputerBean?cumputerBean)?{??
- super();??
- this.deptBean?=?deptBean;??
- this.cumputerBean?=?cumputerBean;??
- }??
- ??
- @Override??
- public?String?toString()?{??
- return?"EmployeeBean?[deptBean="?+?deptBean?+?",?cumputerBean="??
- +?cumputerBean?+?"]";??
- }??
- }??
[java]?view plaincopy?
- ???
配置文件操作:
[html]?view plaincopy?
- ??<?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.xsd">??
- ??
- <!--?电脑bean?-->??
- ??
- <bean?id="cumputerBean"?class="www.csdn.spring.autowire.bean.CumputerBean">??
- <property?name="name"?value="HP6325笔记本"?/>??
- </bean>??
- ??
- <!--?部门bean?-->??
- ??
- <bean?id="deptBean"?class="www.csdn.spring.autowire.bean.DeptBean">??
- <property?name="name"?value="CSDN教育事业部"?/>??
- </bean>??
- ??
- <!--?员工bean?根据EmployeeBean中的?属性名称?bytype?去匹配?-->??
- <bean?id="employeeBean"?class="www.csdn.spring.autowire.bean.EmployeeBean"??
- autowire="constructor">??
- </bean>??
- ??
- </beans>??
说明:
1、当构造器的参数类型在容器中找不全时。
?比如:
???配置文件中只配置了CumpterBean时
?
??
[html]?view plaincopy?
- <?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.xsd">??
- ??
- <!--?电脑bean?-->??
- ??
- <bean?id="cumputerBean"?class="www.csdn.spring.autowire.bean.CumputerBean">??
- ??
- <property?name="name"?value="HP6325笔记本"?/>??
- ??
- </bean>??
- ??
- <!--?员工bean?根据EmployeeBean中的?属性名称?bytype?去匹配?-->??
- ??
- <bean?id="employeeBean"?class="www.csdn.spring.autowire.bean.EmployeeBean"??
- ??
- autowire="constructor">??
- ??
- </bean>??
- ??
- </beans>??
[html]?view plaincopy?
- ???
会出现如下bug:
org.springframework.beans.factory.UnsatisfiedDependencyException:
?Error?creating?bean?with?name?'employeeBean'?defined?in?class?path?resource?[spring-constructors.xml]:?Unsatisfied?dependency?expressed?through?constructor?argument?with?index?0?of?type?[www.csdn.spring.autowire.bean.DeptBean]:?:?
No?qualifying?bean?of?type?[www.csdn.spring.autowire.bean.DeptBean]?found?for?dependency:
?expected?at?least?1?bean?which?qualifies?as?autowire?candidate?for?this?dependency.?Dependency?annotations:?{};?
nested?exception?is?org.springframework.beans.factory.NoSuchBeanDefinitionException:
?No?qualifying?bean?of?type?[www.csdn.spring.autowire.bean.DeptBean]?found?for?dependency:?
expected?at?least?1?bean?which?qualifies?as?autowire?candidate?for?this?dependency.?Dependency?annotations:?{}
Caused?by:?
org.springframework.beans.factory.NoSuchBeanDefinitionException:
?No?qualifying?bean?of?type?[www.csdn.spring.autowire.bean.DeptBean]?found?for?dependency:?
expected?at?least?1?bean?which?qualifies?as?autowire?candidate?for?this?dependency.?Dependency?annotations:?{}
2、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:
[html]?view plaincopy?
- <?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.xsd">??
- ??
- <!--?电脑bean?-->??
- ??
- <bean?id="cumputerBean"?class="www.csdn.spring.autowire.bean.CumputerBean">??
- ??
- <property?name="name"?value="HP6325笔记本"?/>??
- ??
- </bean>??
- ??
- <!--?部门bean?-->??
- ??
- <bean?id="deptBean"?class="www.csdn.spring.autowire.bean.DeptBean">??
- ??
- <property?name="name"?value="CSDN教育事业部"?/>??
- ??
- </bean>??
- ??
- <!--?部门bean?-->??
- ??
- <bean?id="deptBean1"?class="www.csdn.spring.autowire.bean.DeptBean">??
- ??
- <property?name="name"?value="CSDN教育事业部"?/>??
- ??
- </bean>??
- ??
- ??
- <!--?员工bean?根据EmployeeBean中的?属性名称?bytype?去匹配?-->??
- ??
- <bean?id="employeeBean"?class="www.csdn.spring.autowire.bean.EmployeeBean"??
- ??
- autowire="constructor">??
- ??
- </bean>??
- ??
- </beans>??
?
说明:上面配置有两个同样类型的DeptBean但是不会出现bug,原因是在EmployeeBean中构造器接受的参数名称与deptBean一致。
3、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:
[html]?view plaincopy?
- <?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.xsd">??
- ??
- <!--?电脑bean?-->??
- ??
- <bean?id="cumputerBean"?class="www.csdn.spring.autowire.bean.CumputerBean">??
- ??
- <property?name="name"?value="HP6325笔记本"?/>??
- ??
- </bean>??
- ??
- <!--?部门bean?-->??
- ??
- <bean?id="deptBean1"?class="www.csdn.spring.autowire.bean.DeptBean">??
- ??
- <property?name="name"?value="CSDN教育事业部"?/>??
- ??
- </bean>??
- ??
- <!--?部门bean?-->??
- ??
- <bean?id="deptBean2"?class="www.csdn.spring.autowire.bean.DeptBean">??
- ??
- <property?name="name"?value="CSDN教育事业部"?/>??
- ??
- </bean>??
- ??
- <!--?员工bean?根据EmployeeBean中的?属性名称?bytype?去匹配?-->??
- ??
- <bean?id="employeeBean"?class="www.csdn.spring.autowire.bean.EmployeeBean"??
- ??
- autowire="constructor">??
- ??
- </bean>??
- ??
- </beans>??
会出现如下bug(与byType的bug一致):
[java]?view plaincopy?
- org.springframework.beans.factory.UnsatisfiedDependencyException:???
- ??
- Error?creating?bean?with?name?'employeeBean'?defined?in?class?path?resource?[spring-constructors.xml]:??
[java]?view plaincopy?
- Unsatisfied?dependency?expressed?through?constructor?argument?with?index?0?of?type?[www.csdn.spring.autowire.bean.DeptBean]:?:???
- ??
- No?qualifying?bean?of?type?[www.csdn.spring.autowire.bean.DeptBean]?is?defined:???
- ??
- expected?single?matching?bean?but?found?2:?deptBean1,deptBean2;???
- ??
- nested?exception?is?org.springframework.beans.factory.NoUniqueBeanDefinitionException:??
- ??
- ?No?qualifying?bean?of?type?[www.csdn.spring.autowire.bean.DeptBean]?is?defined:???
[java]?view plaincopy?
- expected?single?matching?bean?but?found?2:?deptBean1,deptBean2??