码迷,mamicode.com
首页 > 编程语言 > 详细

Spring之注入复杂类型属性

时间:2017-12-09 15:47:11      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:text   ext.get   xsd   contex   set   参数   framework   key   ica   

注入类:

 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();

 

Spring之注入复杂类型属性

标签:text   ext.get   xsd   contex   set   参数   framework   key   ica   

原文地址:http://www.cnblogs.com/gongxr/p/8011458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!