Spring之注入复杂类型属性

时间:2022-12-17 03:26:05

注入类:

 package helloworld;

 import java.util.List;
import java.util.Map;
import java.util.Properties; public class User {
private List list;
private Map<String, String> map;
private Properties pro; public void setList(List list) {
this.list = list;
} public void setMap(Map<String, String> map) {
this.map = map;
} public void setPro(Properties pro) {
this.pro = pro;
} public void say() {
System.out.println(list);
System.out.println(map);
System.out.println(pro);
}
}

beans.xml配置

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:contexnt="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="user" class="helloworld.User">
<property name="list">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<property name="map">
<map>
<entry key="a" value="apple"></entry>
<entry key="b" value="banner"></entry>
</map>
</property>
<property name="pro">
<props>
<prop key="name">Tom</prop>
<prop key="age">25</prop>
</props>
</property>
</bean>

测试代码:

         ApplicationContext context =  new ClassPathXmlApplicationContext("beans.xml");
// 参数注入
User user = (User) context.getBean("user");
user.say();