Spring — Autowiring using @Autowired annotation | Code Factory

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 …
<!-- <beans xmlns="http://www.springframework.org/schema/beans"
http://www.springframework.org/schema/context/spring-context.xsd"> -->
<!-- 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 id="e2" class="com.codeFactory.beans.Engine">
<property name="modelYear" value="2019" />
package com.codeFactory.beans;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
* @author code.factory
public class Car {
private Engine engine;
public void print() {
System.out.println("Engine - Model Year " + engine.getModelYear());
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;
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");
Engine - Model Year 2020




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The 3 Realizations That Made Me a Better Programmer

Deployment View of Public and Private APIs in CloudHub

Remediate Cyber-Threat via Serverless Framework AWS Lambda

Kotlin Advanced Programming

On first looking into Sandi Metz’s POODR: Her thoughts on Object Oriented Programming and the Agile…

Traits over Interfaces

6 amazing web development trends to be witnessed in 2018–19

Data Scraping Tutorial: an easy project for beginners.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Junit 5 @Nested

Return nothing, with Java Optionals

What is java?

Java 11 : newbie