按类型将引用bean自动装配到列表中

时间:2011-09-16 14:36:06

标签: java spring

我有一个类,其中包含Daemon类型的对象列表。

class Xyz {    
    List<Daemon> daemons;
}

我的弹簧配置如下所示。

<bean id="xyz" class="package1.Xyz">
   <property name="daemons" ref="daemonsList">
</bean>

<bean id="daemon1" class="package1.DaemonImpl1"/>
<bean id="daemon2" class="package1.DaemonImpl2"/>

<bean id="daemonsList" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <ref bean="daemon1" />      
                <ref bean="daemon2" />
            </list>
        </constructor-arg>
</bean>

现在,不是在列表中显式连接每个守护程序实现,而是可以在列表中自动自动装载Daemon类型的所有bean。我想解决的问题是,如果有人创建了Daemon类的新实现的bean并忘记将其连接到列表中。

我在stackoverflow上的某个地方看到过这个问题,但是无法再找到它。为此道歉。

2 个答案:

答案 0 :(得分:66)

它应该像这样工作(从XML中删除ArrayList bean):

public Class Xyz {    

    private List<Daemon> daemons;

    @Autowired
    public void setDaemons(List<Daemon> daemons){
        this.daemons = daemons;
    }

}

我认为没有办法在XML中执行此操作。


请参阅: 3.9.2. @Autowired and @Inject

  

也可以从中提供特定类型的所有bean   ApplicationContext通过将注释添加到字段或方法中   期望这种类型的数组:

public class MovieRecommender {

  @Autowired
  private MovieCatalog[] movieCatalogs;

  // ...
}
  

同样适用于类型化集合:

public class MovieRecommender {

  private Set<MovieCatalog> movieCatalogs;

  @Autowired
  // or if you don't want a setter, annotate the field
  public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
      this.movieCatalogs = movieCatalogs;
  }

  // ...
}

BTW,从Spring 4.x开始,these lists can be ordered automatically using the @Ordered mechanism

答案 1 :(得分:1)

好吧,这可以通过Spring Documentation中所述的两种方法来实现。

下面是文档摘录。

  

通过byType或构造函数自动装配模式,您可以连接数组和类型化的集合。

1。 autowire =“ byType”

如果xml中定义的bean的类型与列表的类型匹配,则可以使用“ byType”自动装配。

示例:

Motor.java

package com.chiranth;
public interface Motor 
{
   public void start();
}

ElectricMotor1.java

package com.chiranth;
public class ElectricMotor1 implements Motor
{
     public void start() 
     { 
         System.out.println("Motor 1 Started.");
     }
}

ElectricMotor2.java

package com.chiranth;
public class ElectricMotor2 implements Motor
{
    public void start() 
    {
        System.out.println("Motor 2 Started.");
    }
}

TeslaModelX.java

package com.chiranth;
import java.util.List;
public class TeslaModelX 
{
    private List<Motor> motor;

    public List<Motor> getMotor()
    {
        return motor;
    }

    public void setMotor(List<Motor> motor) 
    {
        this.motor = motor;
    }

    public void goForward()
    {
        for(Motor m :motor)
            m.start();
        System.out.println("Going Forward.");
    }
}

Spring.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:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="electricMotor1" class="com.chiranth.ElectricMotor1"/>
    <bean id="electricMotor2" class="com.chiranth.ElectricMotor2"/>

    <bean id="modelX" class="com.chiranth.TeslaModelX" autowire="byType"/>
</beans>

Test.java

package com.chiranth;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test 
{
    public static void main(String[] args) 
    {
        ApplicationContext context= new ClassPathXmlApplicationContext("Spring.xml");
        TeslaModelX modelx=(TeslaModelX)context.getBean("modelX");
        modelx.goForward();
    }
}

输出:

Motor 1 Started.
Motor 2 Started.
Going Forward.

2。 autowire =“ constructor”

如果xml中定义的bean的类型与构造函数中参数的类型匹配,则可以使用“构造函数”实现自动装配。

示例:

考虑上述Motor.java,ElectricMotor1.java和ElectricMotor2.java。

TeslaModelX.java

package com.chiranth;
import java.util.List;
public class TeslaModelX 
{
    private List<Motor> motor;

    public TeslaModelX(List<Motor> motor)
    {
        this.motor=motor;
    }

    public void goForward()
    {
        for(Motor m:motor)
            m.start();
        System.out.println("Going Forward.");
    }
}

Spring.xml     

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="electricMotor1" class="com.chiranth.ElectricMotor1"/>
    <bean id="electricMotor2" class="com.chiranth.ElectricMotor2"/>

    <bean id="modelX" class="com.chiranth.TeslaModelX" autowire="constructor"/>
</beans>

Test.java

package com.chiranth;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test 
{
    public static void main(String[] args) 
    {
        ApplicationContext context= new ClassPathXmlApplicationContext("Spring.xml");
        TeslaModelX modelX=(TeslaModelX)context.getBean("modelX");
        modelX.goForward();
    }
}

输出:

Motor 1 Started.
Motor 2 Started.
Going Forward.