Spring Autowiring with @Autowired Annotation

Autowiring with annotations isn’t much different than using the autowire attribute in XML. But it does allow for more fine-grained autowiring, where you can selectively annotate certain properties for autowiring.

Annotation wiring isn’t turned on in the Spring container by default. So, before we can use annotation-based autowiring, we’ll need to enable it in our Spring configuration. The simplest way to do that is with the <context:annotation-config> element from Spring’s context configuration namespace:

<?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-3.0.xsd">
 
    <context:annotation-config/>
    
     <!-- bean declarations go here-->

</beans>

@Autowired on Setter Methods

Now you can get rid of the element that wires the Address with an employee. When Spring sees that you’ve annotated setAddress() with @Autowired it’ll try to perform byType autowiring on the method.

@Autowired
public void setAddress(Address address) {
    this.address = address;
}

What’s especially interesting about @Autowired is that you don’t have to use it with a setter method. You can use it on any method to automatically wire in bean references:

@Autowired
public void yourAddress(Address address) {
    this.address = address;
}

@Autowired on Properties

What’s more, you can directly annotate properties and do away with the setter methods altogether:

@Autowired
private Address address;

@Autowired on Constructors

When used with constructors, @Autowired indicates that the constructor should be autowired when creating the bean, even if no <constructor-arg> elements are used to configure the bean in XML.

@Autowired
public Employee(Address address) {
    this.address = address;
}

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
 
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency>

</dependencies>

1. POJO

Now create Employee Class as following.

package com.kruders.bean;

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

public class Employee {
	private String name;
	private Address address;
	
	public Employee() {
	}
	
	public Employee(Address address) {
		this.address = address;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Address getAddress() {
		return address;
	}
	
	@Autowired
	public void setAddress(Address address) {
		this.address = address;
	}
}

Now create Address Class as following

package com.kruders.bean;

public class Address {
	private String city;
	private String country;
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
}

2. Configuration

Create Spring-Bean.xml and write the following code

<?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-3.0.xsd">

    <context:annotation-config/>
	
    <bean id="employee" class="com.kruders.bean.Employee">
        <property name="name" value="Puneet" />
    </bean>
		
    <bean id="address" class="com.kruders.bean.Address">
        <property name="city" value="Delhi" />
	<property name="country" value="India" />
    </bean>

</beans>

3. Run Program

Create Main.java class that displays the employee and address details and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.bean.Employee;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"Spring-Beans.xml");
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		System.out.println("City " + employee.getAddress().getCity());
		System.out.println("State " + employee.getAddress().getCountry());
	}

}

When you run the above example you’ll get an output like:

Name Puneet
City Delhi
State India

The folder structure of the example is shown below in Figure 27.1

Figure 27.1 Figure 27.1

You can download the source code of this example here.

No comments yet.

Leave a Reply