Spring — Primitive Array / Secondary Array DI Parameters | Code Factory

Spring Tutorial Index Page: Link

Create Java Project

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="e1" class="com.codeFactory.beans.Engine">
<property name="modelYear" value="2001" />
</bean>
<bean id="e2" class="com.codeFactory.beans.Engine">
<property name="modelYear" value="2002" />
</bean>
<bean id="e3" class="com.codeFactory.beans.Engine">
<property name="modelYear" value="2003" />
</bean>
<bean id="c" class="com.codeFactory.beans.Car">
<property name="companyNames">
<list>
<value>JLR</value>
<value>Hummer</value>
<value>Audi</value>
</list>
</property>
<property name="engines">
<list>
<ref bean="e1" />
<ref bean="e2" />
<ref bean="e3" />
</list>
</property>
</bean>
</beans>
package com.codeFactory.beans;import java.util.Arrays;/**
* @author code.factory
*
*/
public class Car {
private String companyNames[];
private Engine engines[];
public Car() {
System.out.println("Car.Car()");
}
public void setCompanyNames(String[] companyNames) {
this.companyNames = companyNames;
}
public void setEngines(Engine[] engines) {
this.engines = engines;
}
public void print() {
System.out.println("Company Name " + Arrays.toString(companyNames));
for (Engine engine : engines) {
System.out.println("Engine - Model Year " + engine.getModelYear());
}
}
}
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Engine {
private int modelYear;
public Engine() {
System.out.println("Engine.Engine()");
}
public int getModelYear() {
return modelYear;
}
public void setModelYear(int modelYear) {
this.modelYear = modelYear;
}
}
package com.codeFactory.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.beans.Car;/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");

Car t = (Car) context.getBean("c");
t.print();
}
}
Engine.Engine()
Engine.Engine()
Engine.Engine()
Car.Car()
Company Name [JLR, Hummer, Audi]
Engine - Model Year 2001
Engine - Model Year 2002
Engine - Model Year 2003

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store