Spring学习笔记_装配bean_02
来源:互联网 发布:java离线安装包 32位 编辑:程序博客网 时间:2024/05/17 04:05
Spring的属性的注入包括单个properties的注入,list,set,map,properties集中类型。下面是代码示例。
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] ******钢琴表演。。。******
0 0
- Spring学习笔记_装配bean_02
- Spring学习笔记_装配bean_01
- Spring学习笔记-自动装配
- spring学习笔记(自动装配)
- 5.Spring学习笔记_自动装配(by尚硅谷_佟刚)
- Spring学习笔记-1:Spring装配
- Spring学习笔记-C2-Spring装配Bean
- Spring学习笔记(二) --- 装配Bean之自动化装配
- Spring学习笔记(四) --- 装配Bean之通过XML装配
- Spring学习笔记(六) --- 装配Bean之高级装配
- (笔记)Spring实战_装配Bean(3)_使用表达式装配
- 《Pro Spring》学习笔记之自动装配
- Spring学习笔记之Bean装配
- Spring学习笔记之Bean的装配
- [学习笔记]spring实战-装配Bean
- Spring学习笔记—装配Bean
- 【Spring学习笔记四】-自动装配Bean
- Spring学习笔记之自动化装配Bean
- 解决不同版本opencv链接库出错问题
- 【C++总结】关于函数返回对象析构的问题
- 前景提取--PBAS算法
- 全文搜索的数据库及实体实现
- 第12周 项目4 点、圆的关系
- Spring学习笔记_装配bean_02
- 如何在PowerShell中把密码保存到文件
- 生产环境超时库存回收优化
- Spring学习总结(2.3)-Spring MVC:handlermapping
- jQuery操作css-position()与offset()方法的区别
- 【javascript】DOM小结
- 北京络捷斯特第三方物流信息系统技术解析(六) 订单录入-加工订单
- Matlab中的dir函数
- [BI项目记]-配置Sharepoint2013支持文档版本管理笔记