Spring Application with Dependency Injection – Setter

Spring Application with Dependency Injection – Setter

Let’s see how to write a simple Spring application with DI using constructor. 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;

<!– mirek bean definition –>

<bean id=”mirek”>
<property name=”mureczek” ref=”murek”/>
<property name=”bureczek” ref=”burek”/>
</bean>

<!– murek bean definition –>

<bean id=”murek”>
</bean>

<!– burek bean definition –>

<bean id=”burek”>
</bean>

</beans>

You can run it and see the output:

gru 11, 2013 2:36:39 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f1bf46: startup date [Wed Dec 11 14:36:39 CET 2013]; root of context hierarchy
gru 11, 2013 2:36:39 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
gru 11, 2013 2:36:39 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1203fe5: defining beans [mirek,murek,burek]; 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.

Leave a comment