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>




参数是List集合类 参数是set集合类
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
原创粉丝点击