Spring注入复杂类型属性
程序员文章站
2022-03-25 12:17:27
...
Spring注入复杂类型属性
‘’
1 数组
2 list集合
3 map集合
4 properties(键值对)类型
配置文件bean1.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">
<!-- 注入复杂类型属性值0 -->
<bean id="person" class="com.cn.ioc.Person">
<!-- 数组 -->
<property name="arrs">
<list>
<value>小王</value>
<value>小明</value>
<value>小李</value>
</list>
</property>
<!-- list-->
<property name="list">
<list>
<value>小zhu</value>
<value>小ming</value>
<value>小li</value>
</list>
</property>
<!-- map -->
<property name="map">
<map>
<entry key="aa" value="lucy"></entry>
<entry key="bb" value="mary"></entry>
<entry key="cc" value="tom"></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>
2
Person.java
package com.cn.ioc;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Person {
private String pname;
private String[] arrs;
private List<String > list;
private Map<String,String > map;
private Properties properties;
public void setPname(String pname) {
this.pname = pname;
}
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void test1(){
System.out.println("arrs"+arrs);
System.out.println("list"+list);
System.out.println("map"+map);
System.out.println("properties"+properties);
}
}
测试类
测试结果
上一篇: MySQL数据库的基本操作
下一篇: Java的mySQL数据库操作