//创建实体类
package com.sq.service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
*@author sq
*
*/
public class User {
private String[] a;
private List<String> b;
private Map<String,String> c;
private Properties properties;
public String[] getA() {
return a;
}
public void setA(String[] a) {
this.a = a;
}
public List<String> getB() {
return b;
}
public void setB(List<String> b) {
this.b = b;
}
public Map<String, String> getC() {
return c;
}
public void setC(Map<String, String> c) {
this.c = c;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "User [a=" + Arrays.toString(a) + ", b=" + b + ", c=" + c
+ ", properties=" + properties + "]";
}
}
配置
<?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="user" class="com.sq.service.User">
<!-- 数组 -->
<property name="a">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<!-- list集合 -->
<property name="b">
<list>
<value>1</value>
<value>1</value>
<value>1</value>
<value>1</value>
</list>
</property>
<!-- map集合 -->
<property name="c">
<map>
<entry key="1" value="1"></entry>
<entry key="2" value="2"></entry>
<entry key="3" value="3"></entry>
<entry key="4" value="4"></entry>
</map>
</property>
<!-- properties -->
<property name="properties">
<props>
<prop key="driverclass">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
</bean>
</beans>
测试
package com.sq.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*@author sq
*
*/
public class Test1 {
@Test
public void test(){
//1.加载spring配置文件,根据创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//2.得到配置对象
User service = (User) context.getBean("user");
System.out.println(service);
}
}