Spring @autowired annotation

Use of @Autowired

Use to Auto-wire particular property in a bean.

Applicability of @Autowired

@Autowired can be applied to

  • Setter Methods
  • Arbitrary Methods
  • Constructors
  • Fields
  • Applicable to typed Collection / Map

Default Behaviour of @Autowired

By Default @Autowired will inject the bean byType. byName injection can be forced by using @Qualifier with @Autowired.

The default behavior is to treat annotated methods/ constructors/ fields as indicating required dependencies(required=true).

Important Points

  • Only one annotated constructor per-class can be marked as required, Multiple non-required constructors can be annotated in a single class.
  • The required attribute of @Autowire is more lenient than @Required annotation.
  • It is handled by BeanPostProcessor Implementation.
  • It can not be used to inject references into BeanPostProcessor or BeanFactoryPostProcessor. As the injection is done by these classes only.
  • It is the same as @Inject annotation. As it is Spring provided JSR303 implementation.

 

Example of @Autowired Annotation

In this example, we will create an Object of Contact class. And get the Object using class name and ID.

Company Class

package bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/*
 *  This Annotation enable to scan this class.
 *  
 *  Value will decide the ID of created bean. This ID can be used to get the bean of this class.
 *  
 *  Object can be get either using class name (Company.class) or ID(companyID)
 */
@Component(value = "companyID")
public class Company {

	@Autowired
	public Company(@Value("VGautam") String companyCEO, Address companyAddress) {
		super();
		this.companyCEO = companyCEO;
		this.companyAddress = companyAddress;
	}

	String companyCEO;

	Address companyAddress;

	public String getCompanyCEO() {
		return companyCEO;
	}

	public void setCompanyCEO(String companyCEO) {
		this.companyCEO = companyCEO;
	}

	public Address getCompanyAddress() {
		return companyAddress;
	}

	public void setCompanyAddress(Address companyAddress) {
		this.companyAddress = companyAddress;
	}

	@Override
	public String toString() {
		return "Company [companyCEO=" + companyCEO + ", companyAddress="
				+ companyAddress + "]";
	}

}

Main Class

package main;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import bean.Company;

public class Main {

	public static void main(String[] args) {

		AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext();

		/*
		 * Below line will scan the given package for Components. It will be
		 * refreshed then to get bean from the same.
		 */
		annotationContext.scan("bean");
		annotationContext.refresh();

		/*
		 * Here Class name is used to get the object of Address class
		 */
		Company company = annotationContext.getBean(Company.class);

		/*
		 * Here ID name is used to get the Object of Company Class
		 */
		Company companyBeanWithID = (Company) annotationContext
				.getBean("companyID");

		System.out.println("Company Object Fetched With ID        :::::"
				+ companyBeanWithID.toString());

		System.out.println("Company Object Fetched With Class Name:::::"
				+ company.toString());

	}

}

Execute the above main class and output would be

Company Object Fetched With ID        :::::Company [companyCEO=VGautam, companyAddress=Address [flatNo=Flat No, bldgNo=2B, streetNo=Street No, city=City, pincode=123456]]
Company Object Fetched With Class Name:::::Compan

One comment

  1. Thanks bro for writting such easy to learn tutotial .Your way to express is very nice. I am greatly inspired by you .keep updating by more tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.