Spring 复杂对象的注入
2016-04-09 00:00
387 查看
摘要: Collection Map 等注入
Spring 复杂对象的注入
复杂对象主要分为有两类:Collection类(包括数组和list)和Map类。
List<String>的注入形式:(数组形式其实是差不多的)
map<String,Integer> 注入:
xml的正确写法:可以装配空值这个很关键。
Spring 复杂对象的注入
复杂对象主要分为有两类:Collection类(包括数组和list)和Map类。
List<String>的注入形式:(数组形式其实是差不多的)
public class OneManBand implements Performer { public OneManBand() { } public void perform() throws PerformanceException { for (Instrument instrument : instruments) { instrument.play(); } } private Collection<Instrument> instruments; private Collection<String> names; public Collection<String> getNames() { return names; } public void setNames(Collection<String> names) { this.names = names; } public void setInstruments(Collection<Instrument> instruments) { this.instruments = instruments; // <co // id="co_injectInstrumentCollection"/> } public static void main(String args[]){ ApplicationContext ac=new ClassPathXmlApplicationContext("com/springinaction/springidol/springidol-context-2.xml"); OneManBand one=(OneManBand)ac.getBean("oneManBand"); System.out.println(one.getNames()); } }
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 说明是getter/setter注入--> <bean id="oneManBand" class="com.springinaction.springidol.OneManBand"> <property name="names"> <list> <!-- 内部bean <bean></bean> --> <!-- 引用 <ref bean="xx"></ref> --> <!-- 常规类型主要就是 就用 value--> <value>1</value> <value>nihao</value> </list> </property> </bean> </beans>
map<String,Integer> 注入:
public class OneManBand implements Performer { private Collection<Instrument> instruments; private Map<String,Integer> prices; public Map<String, Integer> getPrices() { return prices; } public void setPrices(Map<String, Integer> prices) { this.prices = prices; } public static void main(String args[]){ ApplicationContext ac=new ClassPathXmlApplicationContext("com/springinaction/springidol/springidol-context-2.xml"); OneManBand one=(OneManBand)ac.getBean("oneManBand"); System.out.println(one.getPrices()); } }
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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="oneManBand" class="com.springinaction.springidol.OneManBand"> <property name="prices"> <map> <entry key="1" value="2"></entry> <entry key="2" value="3"></entry> </map> </property> </bean> </beans>
相关文章推荐
- Spring 使用其他命名空间的时候要注意的事项
- 基于Spring4 Hibernate4 jersey实现rest风格系统
- elasticsearch结合spring springmvc jest 使用做成web架构
- elasticsearch java api 创建客户端连接(Transport Client)
- maven搭建jersey+Spring4+JPA+Druid+quartz
- 对JavaEE中session的理解
- java设计模式——装饰者模式(代理模式)
- 设计模式--备忘录模式
- 设计模式--观察者模式
- 设计模式--原型模式
- 设计模式--代理模式
- 设计模式--状态模式
- 设计模式--策略模式
- 设计模式--模板方法模式
- 设计模式--访问者模式
- 设计模式--总结
- File相关(创建文件及目录)
- springMVC 和ajax 最先进数据交互方式和 406错误处理
- eclipse中green UML 自动生成类图
- day28-struts2( struts架构分析,ognl表达式,ognl表达式)