This example presents the basic concept of using setter injection in Spring.
The technologies used are :
– Spring 4.3.4
– JDK 1.8
– Maven 3.3.9
You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :
mvn eclipse:eclipse
package javaspringexamples.spring.ioc.setterInjection.xml; /** * * @author mounir.sahrani@gmail.com * */ public class A { private B b; private C c; public B getB() { return b; } public void setB(B b) { this.b = b; } public C getC() { return c; } public void setC(C c) { this.c = c; } @Override public String toString() { return b.toString() + " - " + c.toString(); } }
package javaspringexamples.spring.ioc.setterInjection.xml; /** * * @author mounir.sahrani@gmail.com * */ public class B { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } }
package javaspringexamples.spring.ioc.setterInjection.xml; /** * * @author mounir.sahrani@gmail.com * */ public class C { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } }
package javaspringexamples.spring.ioc.setterInjection.xml; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author mounir.sahrani@gmail.com * */ public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "/javaspringexamples/spring/ioc/setterInjection/xml/xmlConfiguration.xml"); // Getting and printing objectA A objectA = (A) applicationContext.getBean("objectA"); System.out.println(objectA); } }
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <util:map id="objectName"> <entry key="A" value="Object A" /> <entry key="B" value="Object B" /> <entry key="C" value="Object C" /> </util:map> <bean id="objectA" class="javaspringexamples.spring.ioc.setterInjection.xml.A"> <property name="b" ref="objectB" /> <property name="c" ref="objectC" /> </bean> <bean id="objectB" class="javaspringexamples.spring.ioc.setterInjection.xml.B"> <property name="name" value="#{objectName[B]}" /> </bean> <bean id="objectC" class="javaspringexamples.spring.ioc.setterInjection.xml.C"> <property name="name" value="#{objectName[C]}" /> </bean> </beans>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>javaspringexamples.spring</groupId> <artifactId>example-XML-Based-Setter-Injection</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>example XML Based Setter Injection</name> <properties> <springframework.version>4.3.4.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
The output is :
Object B – Object C
Get the sources of the example from the following GitHub url