Spring Autowire beans with @Autowired Annotation

This tutorial shows how to use the @Autowired annotation. The @Autowired annotation marks a constructor, field, setter method or config methods as to be autowired for the spring container.

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.

package com.memorynotfound.spring.core.autowired;

public class Person {

    private String name;
    private Job job;

}

package com.memorynotfound.spring.core.autowired;

public class Job {

    private String name;

}

Enabling @Autowired Annotation

By default the @Autowired annotation is not enabled. So in order to use this annotation we need to register the AutowiredAnnotationBeanPostProcessor. We can do this by explicitly defining this bean in the spring configuration file, or we can include the <context:annotation-config /> or the<context:annotation-config /> element which register the AutowiredAnnotationBeanPostProcessor automatically.

<?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 class="com.memorynotfound.spring.core.autowired.Job">
        <property name="name" value="Java Developer"/>
    </bean>

    <bean class="com.memorynotfound.spring.core.autowired.Person">
        <property name="name" value="John Doe"/>
    </bean>

</beans>

Setter Injection

You can annotate a properties setter method with the @Autowired annotation. This setter method does not have to be public.

package com.memorynotfound.spring.core.autowired;

import org.springframework.beans.factory.annotation.Autowired;

public class Person {

    private String name;
    private Job job;

    public void setName(String name) {
        this.name = name;
    }

    @Autowired
    public void setJob(Job job) {
        this.job = job;
    }
}

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.

package com.memorynotfound.spring.core.autowired;

import org.springframework.beans.factory.annotation.Autowired;

public class Person {

    private String name;
    private Job job;

    @Autowired
    public Person(Job job) {
        this.job = job;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Field Injection

Fields are injected right after construction of a bean and are set via reflection. This means that it’s non mandatory to have a setter/constructor for that property that you are autowiring. This field does not have to be public.

package com.memorynotfound.spring.core.autowired;

import org.springframework.beans.factory.annotation.Autowired;

public class Person {

    @Autowired
    private Job job;
    private String name;

    public void setName(String name) {
        this.name = name;
    }
}

Output

Person{name='John Doe', job=Job{name='Java Developer'}}

References

Download

You may also like...