Setter-based dependency injection by R4R Team

Setter-based dependency injection:-
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

Example
The following example shows a class Test that can only be dependency-injected using pure setter-based injection.

Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

1 Create a project with a name SpringExample and create a package com.r4r.in under the src folder in the created project.
2 Add required Spring libraries using Add External JARs .
3 Create Java classes Test, Message and MainApp under the com.r4r.in.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

1 Create Test.java file.

package com.r4r.in;

public class Test {

private Message msg;

// a setter method to inject the dependency.

public void setMessage(Message msg) {

System.out.println("Inside setMessage." );

this.msg = msg;

}

// a getter method to return spellChecker

public Message getMessage() {

return msg;

}

public void show() {

msg.showmsg();

} }

2 Here you  show Meaasge using of the setter methods. To set a variable msg we are using setMessage() method which is very similar to Java POJO classes. Let us create the content of another dependent class file Message.java:

package com.r4r.in;

public class Message {

public Message(){

System.out.println(" Inside Message constructor." );

}

public void showmsg() {

System.out.println("Inside showmsg." );

} }

3 Create MainApp.java file.

package com.r4r.in;

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");

Test t = (Test) context.getBean("test");

t.show();

} }

4 Create configuration file Beans.xml which has configuration for the setter-based injection:

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

<!-- Definition for test bean -->

<bean id="test" class="com.r4r.in.Test">

<property name="msg" ref="msg"/>

</bean>

<!-- Definition for msg bean -->

<bean id="msg" class="com.r4r.in.Message">

</bean>

</beans>

5 Run the Application if everything is setup then show the message as below:

Inside Message constructor.

Inside setMessage.

Inside showmsg.

The main difference in Beans.xml file defined in constructor-based injection and setter-based injection. The only difference is inside the <bean> element where we have used <constructor-arg> tags for constructor-based injection and <property> tags for setter-based injection.

Second difference is that in case you are passing a reference to an object, you need to use ref attribute of <property> tag and if you are passing a value directly then you should use value attribute.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!