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值 忽略set、get 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); } }
<?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-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd "> <bean id="userDao" class="com.bdqn.dao.UserDaoImpl"></bean> <bean id="userService" class="com.bdqn.service.UserService"> <!-- setter方法 --> <property name="dao" ref="userDao"></property> <!-- 构造方法注入 index编号--> <constructor-arg index="0" ref="userDao"></constructor-arg> <!-- ref引用 --> </bean> <bean id="entity" class="com.bdqn.entity.TestEntity"> <!-- 特殊字符一 --> <property name="specialCharacter1" > <value><![CDATA[&"!\/]]></value> </property> <!-- 特殊字符二 --> <property name="specialCharacter2" > <value>&</value> </property> <!-- 内部bean --> <property name="innerBean"> <bean class="com.bdqn.entity.User"> <property name="username" value="admin"></property> <property name="password" value="123"></property> </bean> </property> <!-- list --> <property name="list"> <list> <value>123</value> <value>456</value> <value>789</value> </list> </property > <!-- 数组类型array --> <property name="array"> <list> <value>a123</value> <value>a456</value> <value>a789</value> </list> </property> <!-- set --> <property name="set"> <set> <value>123</value> <value>456</value> <value>789</value> <value>123</value> <value>456</value> <value>789</value> </set> </property > <!-- map --> <property name="map"> <map> <entry key="a"> <value>a1</value> </entry> <entry key="b"> <value>b1</value> </entry> <!-- 通常用这个 比较直观--> <entry key="c" value="c1"></entry> </map> </property> <!-- Properties类型 --> <property name="props"> <props> <prop key="user">admin</prop> <prop key="uri">/a/b/c.jsp</prop> </props> </property> <!-- 注入空字符串值 --> <property name="emptyValue"><value></value></property> <!-- 注入null值 --> <property name="nullValue"><null></null></property> </bean> </beans>
1 public class EntityTest { 2 3 @Test 4 public void testEntity(){ 5 ApplicationContext ctx= new ClassPathXmlApplicationContext( 6 "applicationContext.xml"); 7 TestEntity entity = (TestEntity) ctx.getBean("entity"); 8 entity.showValue(); 9 } 10 11 }