- Spring DI - Home
- Spring DI - Overview
- Spring DI - Environment Setup
- Spring DI - Create Project
- Spring DI - IOC Containers
- Spring Dependency Injection
Constructor Based Injection Examples
- Spring DI - Constructor Based
- Spring DI - Inner Beans via Constructor
- Spring DI - Collections via Constructor
- Spring DI - Collection Ref via Constructor
- Spring DI - Map via Constructor
- Spring DI - Map Ref via 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 Useful Resources
Spring DI - Autowiring ByType
This mode specifies autowiring by property type. Spring container looks at the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in the configuration file. If matches are found, it will inject those beans. Otherwise, bean(s) will not be wired.
For example, if a bean definition is set to autowire byType in the configuration file, and it contains a spellChecker property of SpellChecker type, Spring looks for a bean definition named SpellChecker, and uses it to set the property. Still you can wire the remaining properties using <property> tags. The following example will illustrate the concept.
Example - Usage of Autowiring ByType
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.
TextEditor.java
Here is the content of TextEditor.java file −
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
private String name;
public void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
SpellChecker.java
Following is 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." );
}
}
MainApp.java
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();
}
}
applicationcontext.xml
Following is the configuration file applicationcontext.xml which has configuration for autowiring byType −
<?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" autowire = "byType">
<property name = "name" value = "Generic Text Editor" />
</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 application. If everything is fine with your application, it will print the following message −
Inside SpellChecker constructor. Inside checkSpelling.