Main Tutorials

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 Author

author image
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

Subscribe
Notify of
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Prateek Bhayana
7 years ago

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

Please Help me out !!

Dhara Patel
7 years ago

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

Krzysztof Kucharczyk
8 years ago

mkyong, awesome job!

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