Spring学习笔记_装配bean_02
2015-05-31 21:52
423 查看
Spring的属性的注入包括单个properties的注入,list,set,map,properties集中类型。下面是代码示例。
1、list的属性注入
applicationContext的配置:
INFO 2015-05-31 21:48:53,235 [message] ******萨克斯表演。。。******
INFO 2015-05-31 21:48:53,235 [message] ******吉它表演。。。******
INFO 2015-05-31 21:48:53,235 [message] ******钢琴表演。。。******
2、Set属性注入跟list集合注入差不多,list允许元素有重复的值,set不能有重复值。xml中的配置也类似。
3、Map属性注入
applicationContext.xml中的配置
1、list的属性注入
applicationContext的配置:
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="saxophone" class="com.csii.spring.Saxophone"></bean> <bean id="guitar" class="com.csii.spring.Guitar"></bean> <bean id="piano" class="com.csii.spring.Piano"></bean> <!-- 注入list集合 --> <bean id="Jack" class="com.csii.spring.OneManBand"> <property name="instruments"> <list> <ref bean="saxophone"/> <ref bean="guitar"/> <ref bean="piano"/> </list> </property> </bean> </beans>然后新建一下几个类:乐器类Instrument.java,钢琴类Piano.java,萨克斯类:Saxophone,吉它类Guitar.java,一人乐队类OneManBand.java
public interface Instrument { public void paly(); }
public class Guitar implements Instrument{ Logger logger=LoggerFactory.getLogger(Saxophone.class); public void paly() { logger.info("******吉它表演。。。******"); } }
public class Saxophone implements Instrument{ Logger logger=LoggerFactory.getLogger(Saxophone.class); public void paly() { logger.info("******萨克斯表演。。。******"); } }
public class Piano implements Instrument{ Logger logger=LoggerFactory.getLogger(Saxophone.class); public void paly() { logger.info("******钢琴表演。。。******"); } }
public class OneManBand implements Performer{ private List<Instrument> instruments; public void perform() throws Exception { for (Instrument instrument : instruments) { instrument.paly(); } } public void setInstruments(List<Instrument> instruments) { this.instruments = instruments; } }然后写一个测试类就可以测试类:
public class Test { public static void main(String[] args) throws Exception { ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); OneManBand oneManBand=(OneManBand)ctx.getBean("Jack"); oneManBand.perform(); } }输出结果:
INFO 2015-05-31 21:48:53,235 [message] ******萨克斯表演。。。******
INFO 2015-05-31 21:48:53,235 [message] ******吉它表演。。。******
INFO 2015-05-31 21:48:53,235 [message] ******钢琴表演。。。******
2、Set属性注入跟list集合注入差不多,list允许元素有重复的值,set不能有重复值。xml中的配置也类似。
<bean id="Jack" class="com.csii.spring.OneManBand"> <property name="instruments"> <set> <ref bean="saxophone"/> <ref bean="guitar"/> <ref bean="piano"/> </set> </property> </bean>
3、Map属性注入
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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="stage" class="com.csii.spring.Stage" factory-method="getInstance"></bean> <bean id="saxophone" class="com.csii.spring.Saxophone"></bean> <bean id="guitar" class="com.csii.spring.Guitar"></bean> <bean id="piano" class="com.csii.spring.Piano"></bean> <!-- Map注入 --> <bean id="Lucy" class="com.csii.spring.OnemanBandMap"> <property name="instruments"> <map> <entry key="SAXOPHONE" value-ref="saxophone"></entry> <entry key="GUITAR" value-ref="guitar"></entry> <entry key="PIANO" value-ref="piano"></entry> </map> </property> </bean> </beans>然后新建一个一人乐队类:OnemanBandMap.java(其它几个类同上面)
public class OnemanBandMap implements Performer{ private Map<String,Instrument> instruments; public void perform() throws Exception { for (String key : instruments.keySet()) { Instrument instrument=instruments.get(key); instrument.paly(); } } public void setInstruments(Map<String, Instrument> instruments) { this.instruments = instruments; } }测试类:
public class Test { public static void main(String[] args) throws Exception { ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); OnemanBandMap lucy=(OnemanBandMap)ctx.getBean("Lucy"); lucy.perform(); } }输出结果:
INFO 2015-05-31 21:55:07,599 [message] ******萨克斯表演。。。****** INFO 2015-05-31 21:55:07,599 [message] ******吉它表演。。。****** INFO 2015-05-31 21:55:07,599 [message] ******钢琴表演。。。******
相关文章推荐
- JAVA单例模式思考
- springMVC基础篇
- java组播MulticastSocket
- Jstl标签库代替 struts2 标签
- java组播MulticastSocket
- java中的浅拷贝与深拷贝
- java.变量
- JAVA基础笔记——Collections与Arrays
- Spring的事件发布机制
- Spring学习笔记_装配bean_01
- Java获取用户ip
- JAVA final关键字,常量的定义
- STRUTS常见面试题
- Eclipse下导入外部jar包的3种方式
- spring整合quartz
- java中Calendar设置时间问题
- Java中Split函数的用法技巧
- 跟我学Java反射——四步曲
- Java常见的排序
- JAVA基础笔记——集合框架