Bean的依赖注入配置
-
Bean的依赖注入方式
-
注入方式 配置方式 通过Bean的set方法注入 <property name="userDAO" ref="userDAO"/> <property name="userDAO" value="userDAO"/>
通过构造Bean的方法进行注入 <constructor-arg name="userDAO" ref="userDAO"/> <constructor-arg name="userDAO" value="userDAO"/>
-
其中,ref是reference的缩写形式,翻译为:涉及,参考的意思,用于引用其它Bean的id,value用于指定属性值
-
-
-
注入数据类型
-
普通数据类型:String、int、boolean,通过value属性指定
-
引用数据类型:UserDAOImpl,通过ref属性指定
-
集合数据类型:List、Map、Properties等
-
普通数据类型的集合
-
配置文件
-
<bean name="UserService" class="com.example.Service.Impl.UserServiceImpl"> <property name="stringList"> <list> <value>aaaa</value> <value>bbbb</value> <value>cccc</value> </list> </property> </bean>
-
-
-
引用数据类型的集合
-
配置文件(两种方式都可以)
-
<property name="DAOList"> <list> <ref bean="DAO1"></ref> <ref bean="DAO2"></ref> <ref bean="DAO3"></ref> <ref bean="DAO4"></ref> </list> </property> </bean> <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl"></bean>
-
<property name="DAOList"> <list> <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl"></bean> </list> </property>
-
-
bean的对应类
-
package com.example.Service.Impl; import com.example.DAO.UserDAO; import com.example.Service.UserService; import java.util.List; public class UserServiceImpl implements UserService { private List<String> stringList; private List<UserDAO> DAOList; public void setStringList(List stringList) { this.stringList = stringList; } public void setDAOList(List daoList) { this.DAOList = daoList; } public void showStringList() { System.out.println(stringList); System.out.println(DAOList); } }
-
-
测试代码
-
package com.example.Test; import com.example.Service.Impl.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserService"); userServiceImpl.showStringList(); } }
-
-
运行结果
-
-
以上介绍的是List集合
下列直接展示Set、Map、Properties
- 配置文件
-
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <bean name="UserService" class="com.example.Service.Impl.UserServiceImpl"> <property name="stringList"> <list> <value>aaaa</value> <value>bbbb</value> <value>cccc</value> </list> </property> <property name="DAOList"> <list> <ref bean="DAO1"></ref> <ref bean="DAO2"></ref> <ref bean="DAO3"></ref> <ref bean="DAO4"></ref> </list> </property> <property name="StringSet"> <set> <value>aaaa</value> <value>bbbb</value> <value>cccc</value> </set> </property> <property name="DAOSet"> <set> <ref bean="DAO1"/> <ref bean="DAO2"/> <ref bean="DAO3"/> <ref bean="DAO4"/> </set> </property> <property name="StringMap"> <map> <entry key="StringMap1" value="aaaa"></entry> <entry key="StringMap2" value="bbbb"></entry> <entry key="StringMap3" value="cccc"></entry> </map> </property> <property name="DAOMap"> <map> <entry key="DAOMap1" value-ref="DAO1"></entry> <entry key="DAOMap1" value-ref="DAO2"></entry> <entry key="DAOMap1" value-ref="DAO3"></entry> <entry key="DAOMap1" value-ref="DAO4"></entry> </map> </property> </bean> <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl"></bean> <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl"></bean> </beans>
-
-
Bean对应的类
-
package com.example.Service.Impl; import com.example.DAO.UserDAO; import com.example.Service.UserService; import java.util.List; import java.util.Map; import java.util.Set; public class UserServiceImpl implements UserService { // todo 无参构造方法 // public UserServiceImpl() { // System.out.println("无参构造方法"); // } // todo 有参构造方法 // public UserServiceImpl(String name) { // System.out.println("有参构造方法"); // System.out.println(name); // } // private UserDAO userDAO; // // public void setUserDAO(UserDAO userDAO) { // } private List<String> stringList; private List<UserDAO> DAOList; private Set<String> StringSet; private Set<UserDAO> DAOSet; private Map<String, String> StringMap; private Map<String, UserDAO> DAOMap; public void setStringList(List stringList) { this.stringList = stringList; } public void setStringSet(Set<String> stringSet) { this.StringSet = stringSet; } public void setDAOList(List daoList) { this.DAOList = daoList; } public void show() { System.out.println("StringList:" + stringList); System.out.println("DAOList:" + DAOList); System.out.println("StringSet:" + StringSet); System.out.println("DAOSet:" + DAOSet); System.out.println("StringMap:" + StringMap); System.out.println("DAOMap:" + DAOMap); } public void setDAOSet(Set daoSet) { this.DAOSet = daoSet; } public void setStringMap(Map stringMap) { this.StringMap = stringMap; } public void setDAOMap(Map daoMap) { this.DAOMap = daoMap; } }
-
-
测试类
-
package com.example.Test; import com.example.Service.Impl.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserService"); userServiceImpl.show(); } }
-
-
运行结果
上述Bean的值都需要自己手动注入,下列展示自动注入
-
Bean的自动装配
- 如果被注入的属性类型是Bean引用的话,那么可以在<bean>标签中使用autowire属性去装配自动注入方式,属性值有两个
- byName:通过属性名自动装配,即去匹配SetXxx与id=“xxx”(name=“xxx”)是否一致;
- byType:通过Bean的类型从容器中匹配,匹配出多个相同的Bean类型时会报错
-
<bean id="userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byType"/>
-
通过byNmae
-
配置文件
-
<bean id="userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byName"/> <bean id="userDAO" class="com.example.DAO.Impl.UserDAOImpl"></bean>
-
-
Bean对应的类
-
在该类总注入UserDAO(代码中体现在SetXxx()方法中)
-
package com.example.Service.Impl; import com.example.DAO.UserDAO; import com.example.Service.UserService; import java.util.List; import java.util.Map; import java.util.Set; public class UserServiceImpl implements UserService { private UserDAO userDAO; public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public void show() { System.out.println(userDAO); } }
-
-
测试类代码
-
package com.example.Test; import com.example.Service.Impl.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userService"); userServiceImpl.show(); Object userDAO = context.getBean("userDAO"); System.out.println(userDAO); } }
-
-
运行结果
- 对比发现,成功通过名称将UserDAO完成了bean对象的注入
- 手动注入配置文件如下
-
<bean id="userService" class="com.example.Service.Impl.UserServiceImpl"> <property name="userDAO"> <ref bean="userDAO"></ref> </property> </bean> <bean id="userDAO" class="com.example.DAO.Impl.UserDAOImpl"></bean>
-
-
通过byType
- 配置文件如下
-
<bean id="userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byType"> </bean> <bean id="userDAO" class="com.example.DAO.Impl.UserDAOImpl"></bean>
-
- 配置文件如下
- 如果被注入的属性类型是Bean引用的话,那么可以在<bean>标签中使用autowire属性去装配自动注入方式,属性值有两个