Autowired annotation consistently useful while doing applications and it can be helped to wire bean setter techniques like @Required annotation. It can be used to wire dependency injections.
Autowired on Setter Methods:
The developer can utilize @Autowired annotations on setter methods to dispose of the <property> in XML file. It requires less code because no need of injecting Dependency.Example as follows.
TextEditor.java
[java]package splessons;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker( ) {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}[/java]
SpellChecker.java
[java]package splessons;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}[/java]
MainApp.java
[java]package splessons;
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");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}[/java]
Beans.xml
[java]<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="splessons.TextEditor">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="splessons.SpellChecker">
</bean>
</beans>[/java]
activates many different annotations in beans, whether they are defined in XML or through component scanning. is for defining beans without using XML.
Output
[java]Inside SpellChecker constructor.
Inside checkSpelling.[/java]
[java]package splessons; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { @Autowired private SpellChecker spellChecker; public TextEditor() { System.out.println("Inside TextEditor constructor." ); } public SpellChecker getSpellChecker( ){ return spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); } }[/java]
Beans.xml
[java]<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="splessons.TextEditor">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="splessons.SpellChecker">
</bean>
</beans>[/java]
Output
[java]Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.[/java]
@Autowired on Constructors
@Autowired useful for constructors als0 and demonstrates that the constructor ought to be authored while establishing the bean, regardless of the fact that no; components are utilized while designing the bean as a part of an XML document. Following is an example.
The content of TextEditor.java file:
[java]package splessons;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}[/java]
The configuration file
Beans.xml
[java]<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="splessons.TextEditor">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="splessons.SpellChecker">
</bean>
</beans>[/java]
Output as follows
[java]Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.[/java]
Autowired with (required=false) option
Below code works only if don't give value for age property but it demands for name property tag.
Student.javafile will be like as follows.
[java]package splessons; import org.springframework.beans.factory.annotation.Autowired; public class Student { private Integer age; private String name; @Autowired(required=false) public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } @Autowired public void setName(String name) { this.name = name; } public String getName() { return name; } }[/java]