Spring DI via constructor

Uses Spring to dependency inject a bean via constructor.

1. IOutputGenerator

An interface and implementation class of it.


package com.mkyong.output;
 
public interface IOutputGenerator
{
	public void generateOutput();
}

package com.mkyong.output.impl;
 
import com.mkyong.output.IOutputGenerator;
 
public class JsonOutputGenerator implements IOutputGenerator
{
	public void generateOutput(){
		System.out.println("This is Json Output Generator");
	}
}

2. Helper class

A helper class, later use Spring to DI the IOutputGenerator, via constructor.


package com.mkyong.output;

import com.mkyong.output.IOutputGenerator;

public class OutputHelper {
	IOutputGenerator outputGenerator;

	public void generateOutput() {
		outputGenerator.generateOutput();
	}

	//DI via constructor
	public OutputHelper(IOutputGenerator outputGenerator){
		this.outputGenerator = outputGenerator;
	}
	
}

3. Spring configuration

See below Spring bean configuration, Spring will DI above “JsonOutputGenerator” into this “OutputHelper” class, via constructor “public OutputHelper(IOutputGenerator outputGenerator)“.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="OutputHelper" class="com.mkyong.output.OutputHelper">
		<constructor-arg>
			<ref bean="JsonOutputGenerator" />
		</constructor-arg>
	</bean>
 
	<bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" />

</beans>

4. Run it

Load everything, and run it.


package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.output.OutputHelper;

public class App {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"SpringBeans.xml");

		OutputHelper output = (OutputHelper)context.getBean("OutputHelper");
    	        output.generateOutput();
	}
}

Output


This is Json Output Generator
Constructor type ambiguities
For constructor that supported multiple parameters, it will lead to common constructor injection type ambiguities issue, please read this solution.

Download Source Code

Download it – Spring-DI-constructor-example.zip (6KB)

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
8 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
Fengtian LumaniArgan OlieRamBabuSpring Tutorial Recent comment authors
newest oldest most voted
Fengtian Lu
Guest
Fengtian Lu

Hi, Mkyong. Why I will get the error message “no default constructor found” when I didn’t define non-arg constructor in the beans?

mani
Guest
mani

How to do same program using annotations?? I want it by end of the day

Argan Olie
Guest
Argan Olie

Once again great (-:

Thanks Mr. Kyong

RamBabu
Guest
RamBabu

Best tuto

trackback
Spring Tutorial

[…] Spring DI via constructor Dependency injection a bean via constructor. […]

Yunus
Guest
Yunus

I would like to say, you are amazing !. Your site is perfect for developer who just like me that is newbie at spring framework.

trackback
Spring Autowiring by Constructor

[…] Spring DI via constructor […]

DB_ICW@ cisco
Guest
DB_ICW@ cisco

In case if we want to use both constructor and setter injection for same class how to do it. I want this ans by end of the day …..