Spring can use @Autowired
annotation to auto wire bean
on the setter method, constructor or a field.
The following code defines two Java beans.
package com.java2s.common; public class JobTitle { @Override public String toString() { return "JobTitle [person=" + person + ", type=" + type + ", action=" + action + "]"; } public Employee getPerson() { return person; } public void setPerson(Employee person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } private Employee person; private int type; private String action; }
Employee Bean
package com.java2s.common; public class Employee{ private String name; private String address; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Employee [name=" + name + ", address=" + address + ", age=" + age + "]"; } }
To enable @Autowired, we have to register 'AutowiredAnnotationBeanPostProcessor'. And we can do that in two ways.
Here is the code to Add Spring context and <context:annotation-config />.
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="myJobTitle" class="com.java2s.common.JobTitle"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="myEmp" class="com.java2s.common.Employee"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
The following code shows how to include 'AutowiredAnnotationBeanPostProcessor' in bean configuration file.
<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-2.5.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="myJobTitle" class="com.java2s.common.JobTitle"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="myEmp" class="com.java2s.common.Employee"> <property name="name" value="java2s" /> <property name="address" value="address ABC" /> <property name="age" value="29" /> </bean> </beans>
Here is the code to run.
package com.java2s.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); JobTitle cust = (JobTitle)context.getBean("myJobTitle"); System.out.println(cust); } }
The following code shows how to autowire bean via @Autowired on setter method
package com.java2s.common; import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Override public String toString() { return "JobTitle [person=" + person + ", type=" + type + ", action=" + action + "]"; } public Employee getPerson() { return person; } @Autowired public void setPerson(Employee person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } private Employee person; private int type; private String action; }
The following code shows how to autowire bean via @Autowired on constructor.
package com.java2s.common; import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Autowired public JobTitle(Employee person) { this.person = person; } @Override public String toString() { return "JobTitle [person=" + person + ", type=" + type + ", action=" + action + "]"; } public Employee getPerson() { return person; } public void setPerson(Employee person) { this.person = person; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } private Employee person; private int type; private String action; }
The following code shows how to autowire bean via @Autowired on a field.
package com.java2s.common; import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Autowired private Employee person; private int type; private String action; }
All of the above three methods autowired Employee Java bean into JobTitle's person property.
Here is the code to run the code above.
package com.java2s.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); JobTitle cust = (JobTitle)context.getBean("myJobTitle"); System.out.println(cust); } }
Output:
By default @Autowired annotation performs the dependency checking to ensure the autowired bean exist.
If Spring can't find a matching bean, it will throw an exception. To disable @Autowired dependency check, set the "required" attribute of @Autowired to false.
import org.springframework.beans.factory.annotation.Autowired; public class JobTitle { @Autowired(required=false) private Employee person; }
@Qualifier annotation allows us to select Java bean to do autowire on a field.
We need @Qualifier annotation when we have two qualified beans to auto wire.
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="myJobTitle" class="com.java2s.common.JobTitle"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> <bean id="myEmp1" class="com.java2s.common.Employee"> <property name="name" value="java2s1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> <bean id="myEmp2" class="com.java2s.common.Employee"> <property name="name" value="java2s2" /> <property name="address" value="address 2" /> <property name="age" value="28" /> </bean> </beans>
In the xml code configuration file above we have two instances of com.java2s.common.Employee. If we do not specify which one to autowire to JobTitle, they both qualified for the autowire action.
To select which Java bean to use we can use the @Qualifier annotation.
package com.java2s.common; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class JobTitle { @Autowired @Qualifier("myEmp1") private Employee person; }