前面我们介绍了四种注入方式,都是在介绍单个值得注入,没有遇到复杂对象,那个car对象不算。今天来看看如果属性是数组,列表,Map和Properties如何注入。
1. 文件准备
还是前面的injection包下新建一个CollectionBean类,里面有一个Demo1.java和一个配置文件
2.数组类型注入
先来看看CollectionBean.java代码内容
package com.anthony.injection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class CollectionBean {
private Object[] arr; //数组类型注入
private List li; //List/set类型注入
private Map map; // Map类型注入
private Properties pro; //properties类型注入
public Object[] getArr() {
return arr;
}
public void setArr(Object[] arr) {
this.arr = arr;
}
public List getLi() {
return li;
}
public void setLi(List li) {
this.li = li;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getPro() {
return pro;
}
public void setPro(Properties pro) {
this.pro = pro;
}
@Override
public String toString() {
return "CollectionBean [arr=" + Arrays.toString(arr) + ", li=" + li + ", map=" + map + ", pro=" + pro + "]";
}
}
关于数组类型注入方式的配置文件内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!-- 数组类型注入 -->
<bean name="cb" class="com.anthony.injection.CollectionBean">
<!-- 如果数组中只准备注入一个对象(值),直接使用value或者ref即可 -->
<property name="arr" value="tom"></property>
</bean>
</beans>
Demo1.java内容
package com.anthony.injection;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo1 {
@Test
public void fun1() {
//1 创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/anthony/injection/applicationContext.xml");
//2 向容器 要 CollectionBean对象
CollectionBean c = (CollectionBean) ac.getBean("cb");
//3 打印CollectionBean对象
System.out.println(c);
}
}
运行结果
CollectionBean [arr=[tom], li=null, map=null, pro=null]
如果数组中需要插入多个值,配置文件这样写
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.anthony.bean.User">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>
<!-- 数组类型注入 -->
<bean name="cb" class="com.anthony.injection.CollectionBean">
<!-- 如果数组中只准备注入一个对象(值),直接使用value或者ref即可
<property name="arr" value="tom"></property>
-->
<!-- array注入多个值 -->
<property name="arr">
<array>
<value>tom</value>
<value>jerry</value>
<ref bean="user" />
</array>
</property>
</bean>
</beans>
运行下Demo1.java,输出
CollectionBean [arr=[tom, jerry, User [name=tom, age=18]], li=null, map=null, pro=null]
这里用到了前面的配置的user这个bean对象。
3.List类型注入
同样,List也分一个值注入和多个值注入,先来看一个值的注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.anthony.bean.User">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>
<!-- List类型注入 -->
<bean name="cb" class="com.anthony.injection.CollectionBean">
<!-- 如果List中只准备注入一个对象(值),直接使用value或者ref即可 -->
<property name="li" value="Lucy"></property>
</bean>
</beans>
运行结果
CollectionBean [arr=null, li=[Lucy], map=null, pro=null]
再来看看多个值的注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.anthony.bean.User">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>
<!-- List类型注入 -->
<bean name="cb" class="com.anthony.injection.CollectionBean">
<!-- 如果List中只准备注入一个对象(值),直接使用value或者ref即可
<property name="li" value="Lucy"></property>
-->
<!-- array注入多个值 -->
<property name="li">
<list>
<value>tom</value>
<value>jerry</value>
<ref bean="user" />
</list>
</property>
</bean>
</beans>
运行结果
CollectionBean [arr=null, li=[tom, jerry, User [name=tom, age=18]], map=null, pro=null]
4.Map类型注入
Map中都是一些键值对,下面看看map对象注入方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.anthony.bean.User">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>
<!-- Map类型注入 -->
<bean name="cb" class="com.anthony.injection.CollectionBean">
<property name="map">
<map>
<entry key="user" value="root"></entry>
<entry key="password" value="123456"></entry>
<entry key="url" value="jdbc:mysql://localhost:3306/j2ee"></entry>
</map>
</property>
</bean>
</beans>
这里模拟JDBC配置信息写一个map对象注入,运行结果如下
CollectionBean [arr=null, li=null, map={user=root, password=123456, url=jdbc:mysql://localhost:3306/j2ee}, pro=null]
来一个map中键值都是高级对象的情况
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.anthony.bean.User">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>
<!-- Map类型注入 -->
<bean name="cb" class="com.anthony.injection.CollectionBean">
<property name="map">
<map>
<entry key="user" value="root"></entry>
<entry key="password" value="123456"></entry>
<entry key-ref="user" value-ref="user"></entry>
</map>
</property>
</bean>
</beans>
运行结果
CollectionBean [arr=null, li=null, map={user=root, password=123456, User [name=tom, age=18]=User [name=tom, age=18]}, pro=null]
5.Properties类型注入
其实Properties就是一个HashMap子类对象,只不过键值对存储都是字符串,下来来一个简单例子。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.anthony.bean.User">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
</bean>
<!-- Properties类型注入 -->
<bean name="cb" class="com.anthony.injection.CollectionBean">
<property name="pro">
<props>
<prop key="user">root</prop>
<prop key="password">123456</prop>
<prop key="url">jdbc:mysql://localhost:3306/j2ee</prop>
</props>
</property>
</bean>
</beans>
运行结果如下
CollectionBean [arr=null, li=null, map=null, pro={user=root, password=123456, url=jdbc:mysql://localhost:3306/j2ee}]