嗯,这可以通过Spring Documentation中所述的两种方法来实现。
以下是文档摘录。
使用byType或构造函数自动装配模式,您可以连接数组和类型化的集合。
1. autowire =“ byType”
如果xml中定义的bean的类型与list的类型匹配,则可以使用“ byType”进行自动装配。
例:
马达
package com.chiranth;
public interface Motor
{
public void start();
}
电机1.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.