Spring – Autowiring using @Autowired annotation | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • * For autowire we can also use @Autowired annotation. We can use @Autowired with property, method, and constructor. In @Autowired there is a also chance to get ambiguity error and to solve it we use @Qualifier(“id”). @Autowired by default use byType. If we use @Autowired then we don’t require to use autowire attribute in xml file.
  • * To activate this @Autowired annotation you must require to create object of AutowiredAnnotationBeanPostProcessor through IOC container. Use spring 3.0 jars to use AutowiredAnnotationBeanPostProcessor and use @Qualifier otherwise it will give NoUniqueBeanDefinitionException.
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
  • If we use @Autowired then no need to have setters or constructors in class file.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create Autowiring-Annotation project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • commons-logging-X.X.jar
    • spring-aop-X.X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
  • You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)
  • You can find namespace information from spring-beans-X.X.X.jar -> META-INF -> spring.schemas
  • * If you use 3.X jars then use dtd and AutowiredAnnotationBeanPostProcessor bean.
  • * If you use 5.X jars then use xml schema and in xml file to activate various annotations to be detected in bean classes: Spring’s @Required and @Autowired …

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<!-- <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"> -->

<beans>
	<!-- uncomment it if you use spring 5.X jars and also use xml schema instead of dtd -->
	<!-- <context:annotation-config /> -->

	<!-- use below bean if you use spring 3.X jars -->
	<!-- activate @Autowired annotation -->
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

	<bean id="c" class="com.codeFactory.beans.Car" />

	<bean id="e1" class="com.codeFactory.beans.Engine">
		<property name="modelYear" value="2020" />
	</bean>

	<bean id="e2" class="com.codeFactory.beans.Engine">
		<property name="modelYear" value="2019" />
	</bean>
</beans>

Car.java

package com.codeFactory.beans;

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

/**
 * @author code.factory
 *
 */
public class Car {

	@Qualifier(value="e1")
	@Autowired(required=true)
	private Engine engine;

	public void print() {
		System.out.println("Engine - Model Year " + engine.getModelYear());
	}
}

Engine.java

package com.codeFactory.beans;

/**
 * @author code.factory
 *
 */
public class Engine {
	private int modelYear;

	public int getModelYear() {
		return modelYear;
	}

	public void setModelYear(int modelYear) {
		this.modelYear = modelYear;
	}

}

Client.java

package com.codeFactory.test;

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

import com.codeFactory.beans.Car;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		Car t = (Car) context.getBean("c");
		t.print();
	}
}

Output:

Engine - Model Year 2020

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s