In my tutorial Bean autowiring in Spring framework, I showed to you all three ways to automatically put a dependency object on a dependent object without having to use the ref or list attributes.
In this tutorial, I will introduce you to another way of doing this, which is using @Autowired annotation. Let’s take a look at how to use this annotation!
I still use the example in the tutorial Bean autowiring in Spring framework for example.
I will remove autowire=”constructor” in the bean room declaration, now if you run the application again:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.springbeanautowiring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Room room = (Room) context.getBean("room"); if (room.getTable() != null) { System.out.println(room.getTable().toString()); } } } |
You will see the table bean is no longer injected into the room bean.
OK, now we will use @Autowired annotation to bring the table bean into the room bean again.
First, we need to declare @Autowired annotation in the Room class for the variable of the Table object. You can declare:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.springbeanautowiring; import org.springframework.beans.factory.annotation.Autowired; public class Room { @Autowired private Table table; public Room() {} public Room(Table table) { this.table = table; } public Table getTable() { return table; } public void setTable(Table table) { this.table = table; } } |
or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.springbeanautowiring; import org.springframework.beans.factory.annotation.Autowired; public class Room { private Table table; public Room() {} @Autowired public Room(Table table) { this.table = table; } public Table getTable() { return table; } public void setTable(Table table) { this.table = table; } } |
or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.springbeanautowiring; import org.springframework.beans.factory.annotation.Autowired; public class Room { private Table table; public Room() {} public Room(Table table) { this.table = table; } public Table getTable() { return table; } @Autowired public void setTable(Table table) { this.table = table; } } |
To use @Autowired annotation, we need to register the AutowiredAnnotationBeanPostProcessor object with Spring. There are two ways we can do it:
– Use <context: annotation-config />
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <bean id="table" class="com.huongdanjava.springbeanautowiring.Table"> <property name="code" value="123456" /> </bean> <bean id="room" class="com.huongdanjava.springbeanautowiring.Room"> </bean> </beans> |
The <context:annotation-config /> tag is declared to use the appropriate objects related to the annotations declared in the instantiated beans in the Spring container.
In this tutorial, I use the <context: annotation-config /> tag to automatically initialize the AutowiredAnnotationBeanPostProcessor object because we are using the @Autowired annotation.
– Use AutowiredAnnotationBeanPostProcessor directly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean id="table" class="com.huongdanjava.springbeanautowiring.Table"> <property name="code" value="123456" /> </bean> <bean id="room" class="com.huongdanjava.springbeanautowiring.Room"> </bean> </beans> |
Result: