标签:work 配置 path 分享 sch xmlns 技术 属性 []
Spring在整合其他框架时在配置文件中可能会涉及到复杂类型属性的注入,以下举例说明:
1.数组类型
2.List集合类型
3.Set集合类型
4.Map集合类型
5.Properties属性类型
直接上代码:
实体类:CollectionBean.java
package com.imooc.ioc.demo5; import java.util.*; /** * Spring复杂类型的集合注入 */ public class CollectionBean { private String[] arrs; //数组类型 private List<String> list; //list类型 private Set<String> set; //set类型 private Map<String , Integer> map; //map类型 private Properties properties; //属性类型 public String[] getArrs() { return arrs; } public void setArrs(String[] arrs) { this.arrs = arrs; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, Integer> getMap() { return map; } public void setMap(Map<String, Integer> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "CollectionBean{" + "arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + ‘}‘; } }
配置文件:applicationContext.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"> <bean id="collectionBean" class="com.imooc.ioc.demo5.CollectionBean"> <!--数组类型注入--> <property name="arrs"> <list> <value>111</value> <value>222</value> <value>333</value> </list> </property> <!--list类型注入--> <property name="list"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> <!--set类型注入--> <property name="set"> <set> <value>ddd</value> <value>eee</value> <value>fff</value> </set> </property> <!--map类型注入--> <property name="map"> <map> <entry key="aaa" value="111"></entry> <entry key="bbb" value="222"></entry> <entry key="ccc" value="333"></entry> </map> </property> <!--properties类型注入--> <property name="properties"> <props> <prop key="aaa">555</prop> <prop key="bbb">666</prop> <prop key="ccc">777</prop> </props> </property> </bean> </beans>
测试类:SpringDemo5.java
package com.imooc.ioc.demo5; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo5 { @Test public void demo1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); CollectionBean collectionContext = (CollectionBean)applicationContext.getBean("collectionBean"); System.out.println(collectionContext); } }
测试类输出结果:
标签:work 配置 path 分享 sch xmlns 技术 属性 []
原文地址:https://www.cnblogs.com/cuijiade/p/9573663.html