×
☰ See All Chapters

Spring @Autowired Annotation

Autowiring means injecting the dependencies automatically to the beans without configuring them in configuration class. In autowiring, spring container dynamically detects and assigns dependent values to bean properties without configuring them explicitly.  Autowiring is possible only for properties which are of user defined data type (no primitive types).  To achieve autowiring we have to use @Autowired annotation. This @Autowired annotation is applied on fields, setter methods, and constructors. The @Autowired annotation injects object dependency implicitly. In order to spring to autowire, dependencies should be available in spring context. Hence such dependencies should have been created made available in spring context. This can be done by instructing spring to scan required dependency classes by using @Component and @ComponentScan annotations. You can read about @Component and @ComponentScan annotations from our chapter Spring @Component Annotation and Spring @ComponentScan Annotation.

The application objects must be aware of one another and communicate with others to get their job done. Example: In an online shopping application, an order management component may need to work with a product management component and a credit card authorization component. All of these will likely need to work with a data access component to read from and write to a database. The act of creating such associations between application objects is commonly referred to as wiring which is the essence of dependency injection. The container will create the objects, wire them together, configure them, and manage their complete lifecycle from cradle to grave (or new to finalize).

Let us see an example for @Autowired Annotation.

Spring @Autowired Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Autowired</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Apple.java

package com.java4coding;

 

import org.springframework.stereotype.Component;

 

@Component

public class Apple{

        public Apple() {

                System.out.println("Apple bean instantiated");

        }

       

        public void printFruitName () {

                System.out.println("Apple");

        }

}

Mango.java

package com.java4coding;

 

import org.springframework.stereotype.Component;

 

@Component

public class Mango {

        public Mango() {

                System.out.println("Mango bean instantiated");

        }

       

        public void printFruitName () {

                System.out.println("Mango");

        }

}

Demo.java

package com.java4coding;

 

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

import org.springframework.context.ApplicationContext;

import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.stereotype.Component;

 

@Component

public class Demo {

       

        @Autowired

        Apple apple;

       

        @Autowired

        Mango mango;

       

        public static void main(String[] args) {

                ApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding");

                Demo demo = (Demo) context.getBean("demo");

                demo.apple.printFruitName();

                demo.mango.printFruitName();

               

                ((ConfigurableApplicationContext)context).close();

        }

}

Output:

spring-autowired-annotation-0
 

All Chapters
Author