Spring之注入复杂类型属性

注入类:

 1 package helloworld;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Properties;
 6 
 7 public class User {
 8     private List list;
 9     private Map<String, String> map;
10     private Properties pro;
11 
12     public void setList(List list) {
13         this.list = list;
14     }
15 
16     public void setMap(Map<String, String> map) {
17         this.map = map;
18     }
19 
20     public void setPro(Properties pro) {
21         this.pro = pro;
22     }
23 
24     public void say() {
25         System.out.println(list);
26         System.out.println(map);
27         System.out.println(pro);
28     }
29 }

 

beans.xml配置

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:contexnt="http://www.springframework.org/schema/context"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/context
 7         http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 8 
 9     <bean id="user" class="helloworld.User">
10         <property name="list">
11             <list>
12                 <value>aaa</value>
13                 <value>bbb</value>
14                 <value>ccc</value>
15             </list>
16         </property>
17         <property name="map">
18             <map>
19                 <entry key="a" value="apple"></entry>
20                 <entry key="b" value="banner"></entry>
21             </map>
22         </property>
23         <property name="pro">
24             <props>
25                 <prop key="name">Tom</prop>
26                 <prop key="age">25</prop>
27             </props>
28         </property>
29     </bean>

 

测试代码:

1         ApplicationContext context =  new ClassPathXmlApplicationContext("beans.xml");
2 //        参数注入
3         User user = (User) context.getBean("user");
4         user.say();

 

posted @ 2017-12-09 14:46  星瑞  阅读(1101)  评论(0编辑  收藏  举报