Spring框架常见的几种数据类的注入方式List Map Array Properties 空值等

废话不多说了,上代码直接。

TestEntity.java

 1 package entity;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Properties;
 6 import java.util.Set;
 7 
 8 public class TestEntity {
 9     private String specialCharacter1; // 特殊字符值1
10     private String specialCharacter2; // 特殊字符值2
11     private User innerBean; // JavaBean类型
12     private List<String> list; // List类型
13     private String[] array; // 数组类型
14     private Set<String> set; // Set类型
15     private Map<String, String> map; // Map类型
16     private Properties props; // Properties类型
17     private String emptyValue; // 注入空字符串值
18     private String nullValue = "init value"; // 注入null值
19 
20     public void setSpecialCharacter1(String specialCharacter1) {
21         this.specialCharacter1 = specialCharacter1;
22     }
23 
24     public void setSpecialCharacter2(String specialCharacter2) {
25         this.specialCharacter2 = specialCharacter2;
26     }
27 
28     public void setInnerBean(User user) {
29         this.innerBean = user;
30     }
31 
32     public void setList(List<String> list) {
33         this.list = list;
34     }
35     
36     public void setArray(String[] array) {
37         this.array = array;
38     }
39 
40     public void setSet(Set<String> set) {
41         this.set = set;
42     }
43 
44     public void setMap(Map<String, String> map) {
45         this.map = map;
46     }
47 
48     public void setProps(Properties props) {
49         this.props = props;
50     }
51 
52     public void setEmptyValue(String emptyValue) {
53         this.emptyValue = emptyValue;
54     }
55 
56     public void setNullValue(String nullValue) {
57         this.nullValue = nullValue;
58     }
59 
60     public void showValue() {
61         System.out.println("特殊字符1:" + this.specialCharacter1);
62         System.out.println("特殊字符2:" + this.specialCharacter2);
63         System.out.println("内部Bean:" + this.innerBean.getUsername());
64         System.out.println("List属性:" + this.list);
65         System.out.println("数组属性[0]:" + this.array[0]);
66         System.out.println("Set属性:" + this.set);
67         System.out.println("Map属性:" + this.map);
68         System.out.println("Properties属性:" + this.props);
69         System.out.println("注入空字符串:[" + this.emptyValue + "]");
70         System.out.println("注入null值:" + this.nullValue);
71     }
72 }

User.java

 1 package entity;
 2 
 3 /**
 4  * 用户实体类
 5  */
 6 public class User implements java.io.Serializable {
 7     private String username; // 用户名
 8 
 9     // getter & setter
10     public String getUsername() {
11         return username;
12     }
13 
14     public void setUsername(String username) {
15         this.username = username;
16     }
17 
18 }

applicationContext.xml核心配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
 6     <bean id="entity" class="entity.TestEntity">
 7         <!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
 8         <property name="specialCharacter1">
 9             <value><![CDATA[P&G]]></value>
10         </property>
11         <!-- 把XML特殊字符替换为实体引用 -->
12         <property name="specialCharacter2">
13             <value>P&amp;G</value>
14         </property>
15         <!-- 给内部引用的类进行注入 -->
16         <property name="innerBean">
17             <bean class="entity.User">
18                 <property name="username">
19                     <value>张三</value>
20                 </property>
21             </bean>
22         </property>
23         <!-- 注入List类型 -->
24         <property name="list">
25             <list>
26                 <value>足球</value>
27                 <value>篮球</value>
28             </list>
29         </property>
30         <!--注入array数组类型 -->
31         <property name="array">
32             <list>
33                 <value>男</value>
34                 <value>女</value>
35                 
36             </list>
37         </property>
38 
39         <!-- 注入set類型 -->
40         <property name="set">
41             <set>
42                 <value>足球</value>
43                 <value>篮球</value>
44             </set>
45         </property>
46         <!-- 注入Map类型 -->
47         <property name="map">
48             <map>
49                 <entry>
50                     <key>
51                         <value>foot</value>
52                     </key>
53                     <value>脚</value>
54                 </entry>
55 
56                 <entry>
57                     <key>
58                         <value>hand</value>
59                     </key>
60                     <value>手</value>
61                 </entry>
62             </map>
63         </property>
64         <!-- 注入Properties类型 這種屬性是讀取.properties配置文件的 -->
65         <property name="props">
66             <props>
67                 <prop key="aaa">呀呀呀</prop>
68                 <prop key="bbb">咪咪咪</prop>
69             </props>
70         </property>
71         <!-- 注入空字符串值 什麽都不寫就是注入了空字符串 -->
72         <property name="emptyValue">
73             <value></value>
74         </property>
75         <!-- 注入null值 -->
76         <property name="nullValue">
77             <null />
78         </property>
79 
80     </bean>
81 </beans>

测试类:


package test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import entity.TestEntity;


public class Test {


@org.junit.Test
public void test() {
  // 使用ApplicationContext接口的实现类ClassPathXmlApplicationContext加载Spring配置文件
  ApplicationContext ctx = new ClassPathXmlApplicationContext(
  "applicationContext.xml");
  TestEntity entity = (TestEntity) ctx.getBean("entity");
  entity.showValue();
  }


}


运行结果:  

特殊字符1:P&G
特殊字符2:P&G
内部Bean:张三
List属性:[足球, 篮球]
数组属性[0]:男
Set属性:[足球, 篮球]
Map属性:{foot=脚, hand=手}
Properties属性:{bbb=咪咪咪, aaa=呀呀呀}
注入空字符串:[]
注入null值:null

原文地址:https://www.cnblogs.com/dongyaotou/p/12116204.html