在Spring中支持注入 数组、List、Map、Set、Properties
注入数组
package com.javaketang.test;
import java.util.Arrays;
public class Data {
private String[] arry;
public String[] getArry() {
return arry;
}
public void setArry(String[] arry) {
this.arry = arry;
}
@Override
public String toString() {
return "data:" + Arrays.toString(this.arry);
}
}
编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="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.xsd">
<bean id="data" class="com.javaketang.test.Data">
<property name="arry">
<array value-type="java.lang.String">
<value>张三</value>
<value>李四</value>
</array>
</property>
</bean>
</beans>
编写测试类
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");
Data data=ap.getBean("data",Data.class);
System.out.println(data);
}
执行,将会输出 data:[张三, 李四]
注入list集合
package com.javaketang.test;
import java.util.List;
public class Data {
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "list:" + list;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="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.xsd">
<bean id="data" class="com.javaketang.test.Data">
<property name="list">
<list value-type="java.lang.String">
<value>张三</value>
<value>李四</value>
</list>
</property>
</bean>
</beans>
测试类
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");
Data data=ap.getBean("data",Data.class);
System.out.println(data);
}
执行后将会输出 list:[张三, 李四]
注入Map集合
package com.javaketang.test;
import java.util.Map;
public class Data {
private Map<String, Object> map;
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
@Override
public String toString() {
return "map:" + map;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="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.xsd">
<bean id="data" class="com.javaketang.test.Data">
<property name="map">
<map key-type="java.lang.String" value-type="java.lang.Object">
<entry key="1" value="李四"></entry>
<entry key="2" value="张三"></entry>
</map>
</property>
</bean>
</beans>
测试类
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");
Data data=ap.getBean("data",Data.class);
System.out.println(data);
}
执行后将会输出 map:{1=李四, 2=张三}
Set集合注入
package com.javaketang.test;
import java.util.Set;
public class Data {
private Set<String> set;
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
@Override
public String toString() {
return "set:" + set;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="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.xsd">
<bean id="data" class="com.javaketang.test.Data">
<property name="set">
<set value-type="java.lang.String">
<value>张三</value>
<value>李四</value>
</set>
</property>
</bean>
</beans>
测试类
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");
Data data=ap.getBean("data",Data.class);
System.out.println(data);
}
执行后将会输出 set:[张三, 李四]
Properties注入
在编写配置文件Properties类型是最常见的。
package com.javaketang.test;
import java.util.Properties;
public class Data {
private Properties prop;
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
@Override
public String toString() {
return "prop:" + prop;
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="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.xsd">
<bean id="data" class="com.javaketang.test.Data">
<property name="prop">
<props>
<prop key="1">张三</prop>
<prop key="2">李四</prop>
</props>
</property>
</bean>
</beans>
测试类
public static void main(String[] args) {
ApplicationContext ap = new ClassPathXmlApplicationContext("application-config.xml");
Data data=ap.getBean("data",Data.class);
System.out.println(data);
}
执行后将会输出 prop:{2=李四, 1=张三}
(中智软件科技学校)