一、处理不同类型的数据:
特殊字符串处理
JavaBean
List
Array
Set
Map
Properties
空字符串
null值
1、创建 TestEntity 实体类
package com.bdqn.cn;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class TestEntity {
private String specialCharacter1; // 特殊字符值1
private String specialCharacter2; // 特殊字符值2
private User innerBean; // JavaBean类型
private List<String> list; // List类型
private String[] array; // 数组类型
private Set<String> set; // Set类型
private Map<String, String> map; // Map类型
private Properties props; // Properties类型
private String emptyValue; // 注入空字符串值
private String nullValue = "init value"; // 注入null值
public void setSpecialCharacter1(String specialCharacter1) {
this.specialCharacter1 = specialCharacter1;
}
public void setSpecialCharacter2(String specialCharacter2) {
this.specialCharacter2 = specialCharacter2;
}
public void setInnerBean(User user) {
this.innerBean = user;
}
public void setList(List<String> list) {
this.list = list;
}
public void setArray(String[] array) {
this.array = array;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProps(Properties props) {
this.props = props;
}
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
public void setNullValue(String nullValue) {
this.nullValue = nullValue;
}
public void showValue() {
System.out.println("特殊字符1:" + this.specialCharacter1);
System.out.println("特殊字符2:" + this.specialCharacter2);
System.out.println("内部Bean:" + this.innerBean.getUsername());
System.out.println("List属性:" + this.list);
System.out.println("数组属性[0]:" + this.array[0]);
System.out.println("Set属性:" + this.set);
System.out.println("Map属性:" + this.map);
System.out.println("Properties属性:" + this.props);
System.out.println("注入空字符串:[" + this.emptyValue + "]");
System.out.println("注入null值:" + this.nullValue);
}
}
2、配置 spring_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="demo" class="com.bdqn.cn.Demo">
<property name="id" value="1"/>
<property name="name" value="zhangsan"/>
</bean>
<bean id="demo2" class="com.bdqn.cn.Demo">
<property name="id" value="2"/>
<property name="name" value="lisi"/>
</bean>
<bean id="demo3" class="com.bdqn.cn.Demo">
<constructor-arg><value type="int">3</value></constructor-arg>
<constructor-arg><value>tom</value></constructor-arg>
</bean>
<bean id="author" class="com.bdqn.cn.Author">
<property name="name" value="夏洛特"/>
<property name="age" value="31"/>
<property name="sex" value="女"/>
</bean>
<bean id="book" class="com.bdqn.cn.Book">
<property name="name" value="简爱"/>
<property name="author" ref="author"/>
</bean>
<bean id="entity" class="com.bdqn.cn.TestEntity">
<!--使用<![CDATA[]]>标记处理XML特殊字符-->
<property name="specialCharacter1">
<value><![CDATA[P&G]]></value>
</property>
<!--把xml特殊字符替换为实体引用-->
<property name="specialCharacter2">
<value>P&</value>
</property>
<!--定义内部Bean-->
<property name="innerBean">
<bean class="com.bdqn.cn.User">
<property name="username">
<value>Mr.Inner</value>
</property>
</bean>
</property>
<!--注入List类型-->
<property name="list">
<list>
<!--定义list中的元素-->
<value>足球</value>
<value>蓝球</value>
</list>
</property>
<!--注入数组类型-->
<property name="array">
<list>
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!--Set类型-->
<property name="set">
<set>
<value>足球</value>
<value>篮球</value>
</set>
</property>
<!--Map类型-->
<property name="map">
<map>
<!--定义Map中的键值对-->
<entry>
<key>
<value>football</value>
</key>
<value>足球</value>
</entry>
<entry>
<key>
<value>basketball</value>
</key>
<value>篮球</value>
</entry>
</map>
</property>
<property name="props">
<props>
<!--定义Properties中的键值对-->
<prop key="football">足球</prop>
<prop key="basketball">篮球</prop>
</props>
</property>
<!--注入空字符串值-->
<property name="emptyValue">
<value></value>
</property>
<!--注入null值-->
<property name="nullValue">
<null/>
</property>
</bean>
</beans>
3、Test1测试
@Test
public void testzhujie(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring_zhujie.xml");
UserService userService = (UserService)context.getBean("userService");
String pwd = userService.getPwd("admin");
System.out.println(pwd);
}
4、测试结果
特殊字符1: P&G
特殊字符2: P&G
内部Bean: admin
userBean: 秋风抚水
List属性: [足球, 篮球]
数组属性[0]:足球
Set属性: [足球, 篮球]
Map属性: {football-足球, basketball=篮球}
Properties属性: football- 足球,basketbal1=篮球}
注入空字符串,口]
注入nul1值: null