2016-04-30 43 views
1

我正在学习春天(和跟随在行动中的春天),因为我在玩弄了解几个概念;然后我观察到以下行为。春天autowire byType与util:列表

场景:
我想用“byType的”自动装配选项自动装配在一类列表属性。所以我创建了util:list bean;它成功地注入了...所以这里没有什么惊喜:)。
然后,我添加了一个新的列表属性在同一个类中(不同的类层次结构:off-course);那么也可以在不做任何额外布线的情况下进行注入....:O

任何机构都可以解释引擎盖下发生了什么。
下面是一些代码snippt。

public interface Performer { 
    public void perform(); 
} 

public interface Instrument { 
    public void play(); 
} 


public class Show implements Audotorium { 
    Logger logger = Logger.getLogger(Show.class.getName()); 

    List<Performer> performerList; 
    List<Instrument> instruments; 

    public void setPerformerList(List<Performer> performerList) { 
     this.performerList = performerList; 
    } 

    public void setInstruments(List<Instrument> instruments) { 
     this.instruments = instruments; 
    } 

    public void show() { 
     Iterator<Performer> performerItr = performerList.iterator(); 
     while (performerItr.hasNext()) { 
      performerItr.next().perform(); 
     } 

     Iterator<Instrument> instrumentItr = instruments.iterator(); 
     while (instrumentItr.hasNext()) { 
      instrumentItr.next().play(); 
     } 
    } 
} 

主类

public static void main(String[] args) { 
     ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring-context.xml"); 
     ((Show) context.getBean("show")).show(); 
    } 

豆CONFIGRATION:

<bean id="juggler" class="com.learning.Juggler"/> 
 
    <bean id="magcian" class="com.learning.Magician"/> 
 
    <bean id="kenny" class="com.learning.Instrumentalist"> 
 
     <property name="song" value="bay finger bay finger"/> 
 
     <property name="instrument" ref="saxphone"/> 
 
    </bean> 
 
    <bean id="hunk" class="com.learning.OneManBand" autowire="byType"> 
 
     <property name="song" value="bay finger bay finger"/> 
 
     <property name="instruments"> 
 
      <list> 
 
       <ref bean="saxphone"/> 
 
       <ref bean="guitar"/> 
 
       <ref bean="harmonica"/> 
 
      </list> 
 
     </property> 
 
    </bean> 
 
    <bean id="totalBeanBags" class="java.lang.Integer" factory-method="valueOf"> 
 
     <constructor-arg value="#{performerList.size()}"/> 
 
    </bean> 
 

 

 
    <bean id="saxphone" class="com.learning.SaxPhone"/> 
 
    <bean id="guitar" class="com.learning.GenaricInstument"> 
 
     <property name="instrumentSound" value="guitar sound"/> 
 
    </bean> 
 
    <bean id="harmonica" class="com.learning.GenaricInstument"> 
 
     <property name="instrumentSound" value="harmonica sound"/> 
 
    </bean> 
 

 

 
    <util:list id="performerList"/> 
 

 
    <bean id="show" class="com.learning.Show" autowire="byType"> 
 

 
    </bean>

输出:

JUGGLING 10 BEANBAGS 
Showing some crazy magicSSsssSSSsss..... 
Playing bay finger bay finger : TOO TOO TOO 
Playing bay finger bay finger : TOO TOO TOO 
guitar sound 
harmonica sound 
----Now its music time---- 
TOO TOO TOO 
guitar sound 
harmonica sound 

回答

1

随着

<util:list id="performerList"/> 

您创建一个空表,所以不能用,你可以将其删除。

你有一个Performer'OneManBand'豆。

当spring尝试自动连线setPerformerList时,它会自动创建一个类型为List<Performer>的列表并将其用作参数。 Spring将所有类型为Performer的豆放入该列表中。

Instrument的列表被创建并注入相同的方式。

这是春天的一个非常方便的功能,我用它来建立注册表,收集某种类型的所有豆子并对它们进行操作。

+0

感谢您的回答, – majid