Spring DI via setter method

A simple Spring example to show you how to dependency inject a bean via setter method, the most common used DI method.

1. IOutputGenerator

An interface and implemntation 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 CsvOutputGenerator implements IOutputGenerator {
	public void generateOutput() {
		System.out.println("This is Csv Output Generator");
	}
}

2. Helper class

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


package com.mkyong.output;

import com.mkyong.output.IOutputGenerator;

public class OutputHelper {
	IOutputGenerator outputGenerator;

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

	//DI via setter method
	public void setOutputGenerator(IOutputGenerator outputGenerator) {
		this.outputGenerator = outputGenerator;
	}
}

3. Spring configuration

Configure bean in Spring configuration file, and reference the bean “CsvOutputGenerator” into “OutputHelper”, via property tag, ref attribute.

In this case, Spring will DI the bean “CsvOutputGenerator” into “OutputHelper” class, via setter method “setOutputGenerator(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">
		<property name="outputGenerator" ref="CsvOutputGenerator" />
	</bean>
 
	<bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" />

</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 Csv Output Generator

Download Source Code

Download it – Spring-DI-setter-method-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
3 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Dhara PatelPrateek BhayanaKrzysztof KucharczykSpring Tutorials Recent comment authors
newest oldest most voted
Prateek Bhayana
Guest
Prateek Bhayana

Error: Could not find or load main class com.mkyong.common.App

Please Help me out !!

Dhara Patel
Guest
Dhara Patel

You can Configure your main class..
For that :
->Go to Run Configuration
->Browse Your Project
->Search your main class (com.mkyong.common.App)

trackback
Spring Tutorials

[…] apply the Dependency Injection (DI) design pattern via Setter Injection and Constructor Injection.Spring DI via setter method Dependency injection a bean via setter method.Spring DI via constructor Dependency injection a bean […]

Krzysztof Kucharczyk
Guest
Krzysztof Kucharczyk

mkyong, awesome job!

Btw. Little typo here: “An interface and implemntation class of it.” <—- lost 'e' in implementation