>依赖注入(集合/数组/set/map)
之前我们对于基本数据类型,字符串,引用数据类型,我们采用的ref或者value,对于集合,如何注入,如何创建?
User类中:
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
private String[] food;
public String[] getFood() {
return food;
}
public void setFood(String[] food) {
this.food = food;
}
private Set<String> book;
public Set<String> getBook() {
return book;
}
public void setBook(Set<String> book) {
this.book = book;
}
private Map<String, String> map;
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
Spring配置:
<!-- 集合的注入 -->
<bean id="userList" class="com.langsin.pojo.User" >
<property name="list">
<!-- 创建一个ArrayList的实例对象 -->
<list>
<value>apple</value>
<value>melon</value>
<value>watermelon</value>
<value>banana</value>
</list>
</property>
<property name="food">
<array>
<value>hamburger</value>
<value>melon</value>
<value>watermelon</value>
<value>banana</value>
</array>
</property>
<property name="book">
<set>
<value>西游记</value>
<value>红楼梦</value>
</set>
</property>
<property name="map">
<map>
<entry key="apple" value="苹果"></entry>
<entry key="banana" value="香蕉"></entry>
<entry key="melon">
<value>甜瓜</value>
</entry>
</map>
</property>
</bean>
测试:
@Test
public void getUserList() throws Exception{
String configLocations="SpringConfig.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
User user=context.getBean("userList",User.class);
List<String> list = user.getList();
for (String string : list) {
System.out.println(string);
}
String[] food = user.getFood();
for (String string : food) {
System.out.println(string);
}
Set<?> set=user.getBook();
for (Object object : set) {
System.out.println(object);
}
Map<String, String> map=user.getMap();
Set<String> keySet = map.keySet();
for (String string : keySet) {
System.out.print(string);
System.out.println(map.get(string));
}
}
>util命名空间简化配置
引入util命名空间,添加约束文件(没网不提示的时候可以本地添加):
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
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-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"></beans>
之后可以这样写:
<bean id="userList" class="com.langsin.pojo.User" p:list-ref="fruitList"
p:food-ref="foodList">
</bean>
<!-- p:属性名 ="" -->
<util:list id="fruitList">
<value>apple</value>
<value>melon</value>
<value>watermelon</value>
<value>banana</value>
</util:list>
<!-- 使用util命名空间时,数组和util集合看作一回事 -->
<util:list id="foodList">
<value>hamburger</value>
<value>melon</value>
<value>watermelon</value>
<value>banana</value>
</util:list>
>自动注入(推荐使用byName)
在Spring配置文件中,去掉明显的配置依赖关系。
之前的方式存在的明显配置依赖关系 p:dao-ref="roleDao :
<bean id="roleService" class="com.langsin.service.RoleService" p:dao-ref="roleDao"/>
<bean id="roleDao" class="com.langsin.dao.impl.RoleDaoImpl"/>
更改后:
<!--自动注入的两种方式:
1.按类型byType :当spring加载到属性需要注入,就去当前容器中看是否存在对应类型的Bean,有就注入到属性中去(这个属性可以写在beans上,作为全局的配置)
注意:当一种类型,有多个实现类时,如果自动注入的时接口类型,这种方式就会报错,spring无法知道具体使用哪个实现类。
2.按名称byName:当spring加载到属性需要注入,就去当前容器中看是否存在id名称与属性相同的,而且类型对应的Bean,有就注入到属性中去。
-->
<bean id="roleService" class="com.langsin.service.RoleService" autowire="byType"/>
<bean id="roleDao" class="com.langsin.dao.impl.RoleDaoImpl"/>
<bean id="roleService" class="com.langsin.service.RoleService" autowire="byName"/>
<bean id="dao" class="com.langsin.dao.impl.RoleDaoImpl"/>
(spring对一个属性进行注入时,拿到属性名,利用反射,调用对应的set方法去完成赋值)
因此,使用byName,创建RoleService 时,会根据属性名dao,去找管理的bean中id名为dao的进行注入。
public class RoleService {
private RoleDao dao=null;
public void setDao(RoleDao dao){
this.dao=dao;
}
public void queryRoleList() throws Exception{
dao.queryRoleList();
}
}