- Spring DI Tutorial
- Spring DI - Home
- Spring DI - Overview
- Spring DI - Environment Setup
- Spring DI - IOC Containers
- Spring Dependency Injection
- Spring DI - Create Project
- Constructor Based Injection Examples
- Spring DI - Constructor Based
- Spring DI - Inner Beans Constructor
- Spring DI - Collections Constructor
- Spring DI - Collection Ref Constructor
- Spring DI - Map Constructor
- Spring DI - Map Ref Constructor
- Setter Based Injection Examples
- Spring DI - Setter Based
- Spring DI - Inner Beans Setter
- Spring DI - Collections Setter
- Spring DI - Collection Ref Setter
- Spring DI - Map Setter
- Spring DI - Map Ref Setter
- Autowiring Examples
- Spring DI - Autowiring
- Spring DI - Autowiring ByName
- Spring DI - Autowiring ByType
- Spring DI - Autowiring Constructor
- Factory Method
- Spring DI - Static Factory
- Spring DI - Non-Static Factory
- Spring DI Useful Resources
- Spring DI - Quick Guide
- Spring DI - Useful Resources
- Spring DI - Discussion
Spring DI - Setter-Based
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 TextEditor that can only be dependency-injected using pure setter-based injection.
Let's update the project created in Spring DI - Create Project chapter. We're adding following files −
TextEditor.java − A class containing a SpellChecker as dependency.
SpellChecker.java − A dependency class.
MainApp.java − Main application to run and test.
Here is the content of TextEditor.java file −
package com.tutorialspoint; public class TextEditor { private SpellChecker spellChecker; // a setter method to inject the dependency. public void setSpellChecker(SpellChecker spellChecker) { System.out.println("Inside setSpellChecker." ); this.spellChecker = spellChecker; } // a getter method to return spellChecker public SpellChecker getSpellChecker() { return spellChecker; } public void spellCheck() { spellChecker.checkSpelling(); } }
Here you need to check the naming convention of the setter methods. To set a variable spellChecker we are using setSpellChecker() method which is very similar to Java POJO classes. Let us create the content of another dependent class file SpellChecker.java −
package com.tutorialspoint; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
Following is the content of the MainApp.java file
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml"); TextEditor te = (TextEditor) context.getBean("textEditor"); te.spellCheck(); } }
Following is the configuration file applicationcontext.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 textEditor bean --> <bean id = "textEditor" class = "com.tutorialspoint.TextEditor"> <property name = "spellChecker" ref = "spellChecker"/> </bean> <!-- Definition for spellChecker bean --> <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean> </beans>
You should note the difference in applicationcontext.xml file defined in the constructor-based injection and the 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.
The second important point to note 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.
Output
Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message −
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling.