Spring Bean Autowiring (Annotation) @Autowired

Beans properties can be auto-wired “auto-set” using xml “Spring Bean Autowiring (XML)” or annotation “@Autowired”

Objective

  • How to autowire bean using @Autowired annotation?
  • Different usage of @Autowired?

Environment

  • Eclipse (Indigo)

Libraries

  • Spring Framework

For more info on how to setup Spring application see First Time Spring

The application

We would like to add different kind of shapes (circle, rectangle & triangle) to a box “shapebox”

( 1 ) Java Code

  • Circle.java
  • Rectangle.java
  • Triangle.java
  • ShapeBox.java
  • Main.java

Circle.java + Rectangle.java + Triangle.java

package com.hmkcode.beans.shapes;

public class Circle  {
	public Circle(){		
	}
}
package com.hmkcode.beans.shapes;

public class Rectangle  {

	public Rectangle(){		
	}
}
package com.hmkcode.beans.shapes;

public class Triangle  {

	public Triangle(){		
	}
}

ShapeBox.java

package com.hmkcode.beans;

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

import com.hmkcode.beans.shapes.Circle;
import com.hmkcode.beans.shapes.Rectangle;
import com.hmkcode.beans.shapes.Triangle;

public class ShapeBox {

	//( 1 ) Autowired property

	//autowire property even if no set method is defined!
	@Autowired
	private Circle circle;

	//autowire property 
	//reuqired=false -> it is OK if no Triangle bean exists!
	@Autowired(required=false)
	private Triangle triangle;

	//( 2 ) Autowire constructor argument
	@Autowired
	public ShapeBox(Rectangle rectangle){
		System.out.println("Rectangle has been autowired to constructor");
	}

	//( 3 ) Autowire a method

	//autowire a set method
	//this example shows that cirlce has been set before set method is called
	@Autowired
	public void setCircle(Circle circle) {
		if(this.circle != null ){
			System.out.println("Circle has been autowired to property \""+
					"this setCircle has been called because it has @Autowired\"");
		}
		else{
			this.circle = circle;
			System.out.println("Circle has been autowired to setCircle");
		}
	}

	//autowire a method (NOT necessarily set method)
	//@Qualifier --> autowire bean named "c2"
	@Autowired
	@Qualifier("c2")
	public void drawCircle(Circle circle) {
		System.out.println("Drawing autowired Circle (c2) ");
	}

}

Main.java

package com.hmkcode;

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

public class Main {

	public static ApplicationContext ctx;
	public static void main(String args[]){

		// Laod spring-config.xml file
		ctx = new ClassPathXmlApplicationContext("com/hmkcode/config/spring-config.xml");

	}
}

( 2 ) XML Configuration (spring-config.xml)

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


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />

	<bean id="circle" class="com.hmkcode.beans.shapes.Circle"  />
	<bean id="c2" class="com.hmkcode.beans.shapes.Circle"  />
	<bean id="rectangle" class="com.hmkcode.beans.shapes.Rectangle"  />

	<!--shapeBox  -->
	<bean id="shapeBox" class="com.hmkcode.beans.ShapeBox" />

</beans>

( 3 ) Test Application

You should get the following result

( 4 ) Explain!

  • @Autowired annotation can be added to property, constructor & methods.
  • If a bean is not required “can be null” use @Autowired(required=false).
  • To autowire a specific bean use @Qualifier(“beanName“).

( 5 ) @Inject

@Inject is a standard (not Spring specific) annotation, can be used to autowire beans just like @Autowired

Download Source Code: Autowire.zip