spring 带参 bean的注入
来源:互联网 发布:淘宝茶叶店模板 编辑:程序博客网 时间:2024/05/19 23:09
1、参数注入==》测试类
public class Instrumentalist implements Performer{ public Instrumentalist(){} private String song; private Instrument instrument; @Override public void perform() throws Exception { System.out.print("Playing "+song+":"); instrument.play(); } public String getSong() { return song; } public void setSong(String song) { this.song=song; } public String screamSong() { return song; } public void setInstrument(Instrument instrument) { this.instrument=instrument; }}
2、Instrument类
public interface Instrument { public void play();} public class Piano implements Instrument{ public Piano(){} @Override public void play() { System.out.println("Piano...........play"); }}public class Saxophone implements Instrument{ @Override public void play() { System.out.println("Saxophone..........paly"); }}
3、配置文件命名空间注入
关联注入=====内部参数注入
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"---------------------->定义命名空间 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <beanid="piano"class="com.spring.jdbc.Piano"></bean> <beanid="kenny"class="com.spring.jdbc.Instrumentalist" p:song="JIngle Bells" p:instrument-ref="piano"> </bean></beans>
<beanid="piano"class="com.spring.jdbc.Piano"></bean> <beanid="kenny"class="com.spring.jdbc.Instrumentalist"> <propertyname="song"value="JIngle Bells"/> <!-- 关联注入 --><!-- <property name="instrument"ref="kenny"></property> --> <!-- 内部参数注入 --> <propertyname="instrument"> <beanclass="com.spring.jdbc.Saxophone"></bean> </property> </bean>
public class OneManBand implements Performer{ @Override public void perform() throws Exception { for(Instrumentins:instruments){ ins.play(); } } private Collection<Instrument> instruments; public void setInstruments(Collection<Instrument> instruments){ this.instruments=instruments; }}
public class OneManBand implements Performer{ @Override public void perform() throws Exception { for(Instrumentins:instruments){ ins.play(); } } private Set<Instrument> instruments; public void setInstruments(Set<Instrument> instruments){ this.instruments=instruments; }}
<bean id="piano"class="com.spring.jdbc.Piano"></bean> <bean id="saxophone"class="com.spring.jdbc.Saxophone"/> <bean id="mand"class="com.spring.jdbc.OneManBand"> <property name="instruments"> <list> <ref bean="piano"/> <ref bean="saxophone"/> </list> </property> </bean>
<bean id="piano"class="com.spring.jdbc.Piano"></bean> <bean id="saxophone"class="com.spring.jdbc.Saxophone"/> <bean id="mand"class="com.spring.jdbc.OneManBand"> <property name="instruments"> <set> <ref bean="piano"/> <ref bean="saxophone"/> </set> </property> </bean>
public static void main(String[] args) throws Exception { GenericXmlApplicationContext context=newGenericXmlApplicationContext(); context.load("com/spring/jdbc/applicationContext.xml"); context.refresh(); OneManBandbean= (OneManBand)context.getBean("mand"); bean.perform(); }
Map集合注入
public class OneManBand implements Performer{ @Override public void perform() throws Exception { Set<String> set=instruments.keySet(); Iterator<String> it=set.iterator(); while(it.hasNext()){ String key=it.next(); Instrument ins=instruments.get(key); System.out.println(key); ins.play(); } } private Map<String,Instrument> instruments; publicvoid setInstruments(Map<String,Instrument> instruments){ this.instruments=instruments; }}
<bean id="piano"class="com.spring.jdbc.Piano"></bean> <bean id="saxophone"class="com.spring.jdbc.Saxophone"/> <bean id="mand"class="com.spring.jdbc.OneManBand"> <property name="instruments"> <map> <entry key="PIANO"value-ref="piano"></entry> <entry key="SAXOPHONE"value-ref="saxophone"></entry> </map> </property> </bean>
0 0
- spring 带参 bean的注入
- Spring的bean注入
- 【Spring】 Bean的注入
- Spring的bean注入方式
- spring bean注入的方式
- Spring中bean的注入
- Spring Bean 注入 Servlet 的方法
- Spring方法注入非单例bean的调用
- Spring--多种Bean的注入(上)
- Spring--多种Bean的注入(中)
- Spring--多种Bean的注入(下)
- spring 注入bean 时的初始化
- 利用spring的表达式语言注入bean
- Spring Bean 注入 Servlet 的方法
- spring框架bean注入的时机问题
- Servlet中注入Spring的bean
- spring配置时的bean注入
- Spring注入Bean的几种方式
- Fiddler小技巧之URL替换
- JAVA时间对象---字符串转日期,日期转字符串
- TCP协议
- Mac下SublimeText_编辑保存的Html乱码问题解决
- LeetCode 34 Search for a Range
- spring 带参 bean的注入
- iOS socket网络编程
- Spark DataFrame相关操作
- UGUI之优化(1)-解决RaycastTarget勾选过多的烦恼
- 找工作如何面试
- Android之JNI NDK如何取消native support 重新add native support
- 【技巧】仿某宝网详情页宝贝放大镜效果
- zabbix创建自定义键值和触发器
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale