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

Spring Tutorial Index Page: Link

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-Array project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    - commons-logging-X.X.jar
    - spring-beans-X.X.X.jar
    - spring-context-X.X.X.jar
    - spring-core-X.X.X.jar
    - spring-expression-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)
<!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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Vim, flash movements between windows with EasyMotion

AZURE DATA FACTORY: THE EXPRESSION CONTAINS SELF REFERENCING VARIABLE.

Stater X Houndrace: The New Era of DeFi and GameFi Begins

Parse the Raw HTML with Nokogiri

Will Subspace Network have its own token?

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
Code Factory

Code Factory

More from Medium

Visitor Design Pattern in Java

Exception handling In Java.

Micronaut vs Springboot — Closer Look

What are Spring Containers and Spring Beans? What is Spring Autowiring and Dependency Injection?