spring 带参 bean的注入

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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fjkxyl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值