Spring Application with Dependency Injection – Setter (Inner Beans)

Spring Application with Dependency Injection – Setter (Inner Beans)

Let’s see how to write a simple Spring application with DI using constructor. This time we are going to use inner beans. We have the following files in our project:

MainApp.java

package com.linaittech;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

Mirek mireczek = (Mirek) context.getBean(“mirek”);
mireczek.mirekMethod();

}

}

Mirek.java

package com.linaittech;

public class Mirek {

private Murek mureczek;
private Burek bureczek;

   public void setMureczek(Murek mureczek) {
System.out.println(“We are inside the setMureczek.” );
this.mureczek = mureczek;
}

 public void setBureczek(Burek bureczek){
System.out.println(“We are inside the setBureczek.” );
 this.bureczek = bureczek;
}

public Murek getMureczek() {
return mureczek;
}

public Burek getBureczek(){
return bureczek;
}

public void mirekMethod() {
mureczek.murekMethod();
bureczek.burekMethod();
}

}

Murek.java

package com.linaittech;

public class Murek {

public Murek(){
System.out.println(“We are inside the Murek constructor.” );
}

public void murekMethod() {
System.out.println(“We are inside the murekMethod.” );
}

}

Burek.java

package com.linaittech;

public class Burek {

public Burek(){
System.out.println(“We are inside the Burek constructor.” );
}

public void burekMethod() {
System.out.println(“We are inside the burekMethod.” );
}

}

Beans.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<!– definitions with using inner beans –>
 
<bean id=”mirek”>

<property name=”mureczek”>
<bean id=”murek”/>
</property>

<property name=”bureczek”>
<bean id=”burek”/>
</property>

</bean>

</beans>

You can run it and see the output:

gru 12, 2013 3:10:32 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@44a541: startup date [Thu Dec 12 15:10:32 CET 2013]; root of context hierarchy
gru 12, 2013 3:10:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
gru 12, 2013 3:10:33 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@91762: defining beans [mirek]; root of factory hierarchy
We are inside the Murek constructor.
We are inside the Burek constructor.
We are inside the setMureczek.
We are inside the setBureczek.
We are inside the murekMethod.
We are inside the burekMethod.

The red lines is the most important for us. We injected two methods (murekMethod and burekMethod) from two beans (Murek and Burek) into the Mirek bean. We used the setters in the Mirek bean, and all was managed by Beans.xml. However, here we have beans defined in another bean that is between <property> and </property> elements.

Leave a comment