Spring Application with Dependency Injection – Constructor

Spring Application with Dependency Injection – Constructor

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 Mirek(Murek mureczek, Burek bureczek) {
        
        System.out.println(“We are inside the Mirek constructor.” );
        this.mureczek = mureczek;
        this.bureczek = 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

<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” class=”com.linaittech.Mirek”>
<constructor-arg ref=”murek”/>
<constructor-arg ref=”burek”/>
</bean>

<!– murek bean definition –>

<bean id=”murek” class=”com.linaittech.Murek”>
</bean>

<!– burek bean definition –>

<bean id=”burek” class=”com.linaittech.Burek”>
</bean>

</beans>

You can run it and see the output:

gru 09, 2013 1:19:57 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f1bf46: startup date [Mon Dec 09 13:19:57 CET 2013]; root of context hierarchy
gru 09, 2013 1:19:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
gru 09, 2013 1:19:57 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@19039f5: 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 Mirek constructor.
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 constructor in the Mirek bean, and all was managed by Beans.xml.

Leave a comment