您的位置:首页 > 编程语言 > Java开发

Spring学习总结四——SpringIOC容器四

2016-11-11 22:31 591 查看
一:spring容器给bean对象注入属性值

1:注入基本属性值

a. 创建MessageBean类:

1 /**
2  *
3  */
4 package com.hlcui.dao;
5
6 /**
7  * @author Administrator
8  *
9  */
10 public class MessageBean {
11     private String name;
12
13     private int age;
14
15     private double salary;
16
17     public String getName() {
18         return name;
19     }
20
21     public void setName(String name) {
22         this.name = name;
23     }
24
25     public int getAge() {
26         return age;
27     }
28
29     public void setAge(int age) {
30         this.age = age;
31     }
32
33     public double getSalary() {
34         return salary;
35     }
36
37     public void setSalary(double salary) {
38         this.salary = salary;
39     }
40
41     public String execute() {
42         System.out.println("name=" + name + "\nage=" + age
43                 + "\nsalary=" + salary);
44         return "success";
45     }
46
47 }


b. 在spring容器配置文件中配置MessageBean对象核心代码:

1 <!-- 配置MessageBean对象 -->
2     <bean id="messageBean" class="com.hlcui.dao.MessageBean">
3         <property name="name" value="Jack"></property>
4         <property name="age" value="27"></property>
5         <property name="salary" value="12000"></property>
6     </bean>


或者将value元素作为property元素的子元素,效果是一样的

1 <bean id="messageBean" class="com.hlcui.dao.MessageBean">
2         <property name="name">
3             <value>Tom</value>
4         </property>
5         <property name="age">
6             <value>27</value>
7         </property>
8         <property name="salary">
9             <value>12000</value>
10         </property>
11     </bean>


c. 运行测试方法

1 @Test
2     public void testBaseInject(){
3         ApplicationContext ac = getApplicationContext();
4         MessageBean mb = ac.getBean("messageBean", MessageBean.class);
5         mb.execute();
6     }




通过运行结果可知属性值已经注入到对象中,这种方式在前面已经总结过,就是setter注入。

2:注入集合属性值(List、Set、Map、Properties)

修改MessageBean类:

1 /**
2  *
3  */
4 package com.hlcui.dao;
5
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Properties;
10 import java.util.Set;
11 import java.util.Map.Entry;
12
13 /**
14  * @author Administrator
15  *
16  */
17 public class MessageBean {
18     private List<String> someList;
19     private Set<Integer> someSet;
20     private Map<String,Object> someMap;
21     private Properties props;
22
23     private String name;
24
25     private int age;
26
27     private double salary;
28
29
30     public List<String> getSomeList() {
31         return someList;
32     }
33
34     public void setSomeList(List<String> someList) {
35         this.someList = someList;
36     }
37
38     public Set<Integer> getSomeSet() {
39         return someSet;
40     }
41
42     public void setSomeSet(Set<Integer> someSet) {
43         this.someSet = someSet;
44     }
45
46     public Map<String, Object> getSomeMap() {
47         return someMap;
48     }
49
50     public void setSomeMap(Map<String, Object> someMap) {
51         this.someMap = someMap;
52     }
53
54     public Properties getProps() {
55         return props;
56     }
57
58     public void setProps(Properties props) {
59         this.props = props;
60     }
61
62     public String getName() {
63         return name;
64     }
65
66     public void setName(String name) {
67         this.name = name;
68     }
69
70     public int getAge() {
71         return age;
72     }
73
74     public void setAge(int age) {
75         this.age = age;
76     }
77
78     public double getSalary() {
79         return salary;
80     }
81
82     public void setSalary(double salary) {
83         this.salary = salary;
84     }
85
86     public String execute() {
87         System.out.println("name=" + name + "\nage=" + age
88                 + "\nsalary=" + salary);
89         System.out.println("--------List集合结果--------");
90         for(String str:someList){
91             System.out.println(str);
92         }
93         System.out.println("--------Set集合结果--------");
94         for(Iterator<Integer> it = someSet.iterator();it.hasNext();){
95             System.out.println(it.next());
96         }
97         System.out.println("-------Map集合结果--------");
98         for(Entry<String, Object> entry:someMap.entrySet()){
99             String key = entry.getKey();
100             String value = (String) entry.getValue();
101             System.out.println(key+"=="+value);
102         }
103         System.out.println("-------Properties结果------");
104         for(Object obj : props.keySet()){
105             String value = props.getProperty((String) obj);
106             System.out.println(obj+"=="+value);
107         }
108         return "success";
109     }
110
111 }


配置文件修改如下:

1 <bean id="messageBean" class="com.hlcui.dao.MessageBean">
2         <property name="name">
3             <value>Tom</value>
4         </property>
5         <property name="age">
6             <value>27</value>
7         </property>
8         <property name="salary">
9             <value>12000</value>
10         </property>
11         <property name="someList">
12             <list>
13                 <value>aaaaList</value>
14                 <value>bbbbList</value>
15                 <value>ccccList</value>
16             </list>
17         </property>
18         <property name="someSet">
19             <set>
20                 <value>111</value>
21                 <value>222</value>
22                 <value>333</value>
23             </set>
24         </property>
25         <property name="someMap">
26             <map>
27                 <entry key="1" value="java"></entry>
28                 <entry key="2" value="c++"></entry>
29                 <entry key="3" value="c#"></entry>
30             </map>
31         </property>
32         <property name="props">
33             <props>
34                 <prop key="username">root</prop>
35                 <prop key="password">root</prop>
36             </props>
37         </property>
38     </bean>
39


运行测试方法:



从结果可以看出,参数注入成功!!!

3:引用方式注入集合属性

修改配置文件内容如下:

1 <util:list id="someList">
2         <value>aaaaList</value>
3         <value>bbbbList</value>
4         <value>ccccList</value>
5     </util:list>
6     <util:set id="someSet">
7         <value>111</value>
8         <value>222</value>
9         <value>333</value>
10     </util:set>
11     <util:map id="someMap">
12         <entry key="1" value="java"></entry>
13         <entry key="2" value="c++"></entry>
14         <entry key="3" value="c#"></entry>
15     </util:map>
16     <util:properties id="props">
17         <prop key="username">root</prop>
18         <prop key="password">root</prop>
19     </util:properties>
20     <bean id="messageBean" class="com.hlcui.dao.MessageBean">
21         <property name="name">
22             <value>Tom</value>
23         </property>
24         <property name="age">
25             <value>27</value>
26         </property>
27         <property name="salary">
28             <value>12000</value>
29         </property>
30         <property name="someList" ref="someList"></property>
31         <property name="someSet" ref="someSet"></property>
32         <property name="someMap" ref="someMap"></property>
33         <property name="props" ref="props"></property>
34 </beans>


运行测试方法:



和之前的效果是一样的,只不过这样配置更加明了!

二:利用spring表达式注入属性值

1:在src下添加文件user.properties,内容如下:

1 name=Jack
2 age=30
3 salary=15000


2:在spring容器配置文件中加载配置文件

1 <!-- 加载配置文件user.properties -->
2     <util:properties id="user" location="classpath:user.properties"/>
3


1 <!-- 配置MessageBean对象 -->
2     <bean id="messageBean" class="com.hlcui.dao.MessageBean">
3         <property name="name" value="#{user.name}"></property>
4         <property name="age" value="#{user.age}"></property>
5         <property name="salary" value="#{user.salary}"></property>
6     </bean>


3:运行测试方法



通过结果可知spring表达式注入成功!

三:spring注入空值

1:修改配置文件

1 <!-- 配置MessageBean对象 -->
2     <bean id="messageBean" class="com.hlcui.dao.MessageBean">
3         <property name="name">
4             <null/>
5         </property>
6         <property name="age" value="#{user.age}"></property>
7         <property name="salary" value="#{user.salary}"></property>
8     </bean>


2:运行测试方法



空值注入成功,只有引用数据类型才可以注入空值!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: