- Spring DI - Discussion
- Spring DI - Useful Resources
- Spring DI - Quick Guide
- Spring DI - Non-Static Factory
- Spring DI - Static Factory
- Spring DI - Autowiring Constructor
- Spring DI - Autowiring ByType
- Spring DI - Autowiring ByName
- Spring DI - Autowiring
- Spring DI - Map Ref Setter
- Spring DI - Map Setter
- Spring DI - Collection Ref Setter
- Spring DI - Collections Setter
- Spring DI - Inner Beans Setter
- Spring DI - Setter Based
- Spring DI - Map Ref Constructor
- Spring DI - Map Constructor
- Spring DI - Collection Ref Constructor
- Spring DI - Collections Constructor
- Spring DI - Inner Beans Constructor
- Spring DI - Constructor Based
- Spring DI - Create Project
- Spring Dependency Injection
- Spring DI - IOC Containers
- Spring DI - Environment Setup
- Spring DI - Overview
- Spring DI - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring DI - Constructor-Based
Constructor-Based DI is accomppshed when the container invokes a class constructor with a number of arguments, each representing a dependency on the other class.
Example
The following example shows a class TextEditor that can only be dependency-injected with constructor injection.
Let s update the project created in
chapter. We re adding following files −TextEditor.java − A class containing a SpellChecker as dependency.
SpellChecker.java − A dependency class.
MainApp.java − Main apppcation to run and test.
Here is the content of TextEditor.java file −
package com.tutorialspoint; pubpc class TextEditor { private SpellChecker spellChecker; pubpc TextEditor(SpellChecker spellChecker) { System.out.println("Inside TextEditor constructor." ); this.spellChecker = spellChecker; } pubpc void spellCheck() { spellChecker.checkSpelpng(); } }
Following is the content of another dependent class file SpellChecker.java
package com.tutorialspoint; pubpc class SpellChecker { pubpc SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } pubpc void checkSpelpng() { System.out.println("Inside checkSpelpng." ); } }
Following is the content of the MainApp.java file.
package com.tutorialspoint; import org.springframework.context.ApppcationContext; import org.springframework.context.support.ClassPathXmlApppcationContext; pubpc class MainApp { pubpc static void main(String[] args) { ApppcationContext context = new ClassPathXmlApppcationContext("apppcationcontext.xml"); TextEditor te = (TextEditor) context.getBean("textEditor"); te.spellCheck(); } }
Following is the configuration file apppcationcontext.xml which has configuration for the constructor-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"> <constructor-arg ref = "spellChecker"/> </bean> <!-- Definition for spellChecker bean --> <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean> </beans>
Output
Once you are done creating the source and bean configuration files, let us run the apppcation. If everything is fine with your apppcation, it will print the following message −
Inside SpellChecker constructor. Inside TextEditor constructor. Inside checkSpelpng.Advertisements