The Spring @autowired annotation example shows you how to use the @Autowired annotation to inject a bean with constructor, field or setter method. Here the @Autowired annotation marks a constructor, field or setter method.Then Spring will look up exactly one bean that matches with the property is annotated and autowired automatically.
Create beans
We are using these beans. The Job bean will be autowired in the Person bean. We are showing you three ways you can autowire, via setter, constructor and field injection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javabycode.spring.core.autowired; public class Fruit { private String name; private Country country; public Fruit(Country country) { this.country = country; } public void setName(String name) { this.name = name; } public void setCountry(Country country) { this.country = country; } @Override public String toString() { return "Fruit{" + "name='" + name + '\'' + ", country=" + country + '}'; } } |
Next, we create Country bean which is autowired into the Fruit bean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javabycode.spring.core.autowired; public class Country { private String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Country{" + "name='" + name + '\'' + '}'; } } |
Enabling @Autowired Annotation in Spring configuration
We enable the @Autowired annotation by adding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="fruit" class="com.javabycode.spring.core.autowired.Fruit"> <property name="name" value="Apple"/> </bean> <bean id="country" class="com.javabycode.spring.core.autowired.Country"> <property name="name" value="USA"/> </bean> </beans> |
Now, we together discuss three option Injection method
Option 1: Setter Injection
We can annotate a properties setter method by marking the @Autowired annotation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javabycode.spring.core.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Fruit { private String name; private Country country; public void setName(String name) { this.name = name; } @Autowired public void setCountry(Country country) { this.country = country; } @Override public String toString() { return "Fruit{" + "name='" + name + '\'' + ", country=" + country + '}'; } } |
Option 2: Constructor Injection
You can have only one constructor with the @Autowired annotation per class. This constructor does not have to be public. When there are multiple constructors with the annotation, a BeanCreationException will be thrown at startup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javabycode.spring.core.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Fruit { private String name; private Country country; @Autowired public Fruit(Country country) { this.country = country; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Fruit{" + "name='" + name + '\'' + ", country=" + country + '}'; } } |
Option 3: Field Injection
In Spring, fields are injected right after construction of a bean and set via reflection. This looks a lot simpler than constructor injection or setter injection
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.javabycode.spring.core.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Fruit { private String name; @Autowired private Country country; public void setName(String name) { this.name = name; } @Override public String toString() { return "Fruit{" + "name='" + name + '\'' + ", country=" + country + '}'; } } |
Demo Spring application
Create the main class to test the Spring @autowired annotation example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode.spring.core.autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MySpringApplication { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Fruit person = context.getBean(Fruit.class); System.out.println(person); } } |
Run the main above, the console will print the following message
1 |
Fruit{name='Apple', country=Country{name='USA'}} |
Conclusion: As you see above, the field injection looks more simpler than the construction injection and the setter injection. This is reason why I prefer the field injection than others. One more thing for the constructor injection, the constructors become very messy if the class has many dependencies.
That’s all on the Spring @autowired annotation example
References
Spring @Autowired Doc
@Autowired JavaDoc
AutowiredAnnotationBeanPostProcessor JavaDoc
Download complete source code, click link below
spring-@autowired-annotation-example.zip (304 downloads)