Spring @Autowired Annotation Example

In this section we will learn about @Autowired Annotation. 


We can use the @Autowired to mark a dependency which Spring is going to resolve and inject.

If @Autowired is applied to

  • a field: then the dependency is stored in this field.
  • a setter: then the setter is invoked, with the parameter that is determined by the same algorithm like for the field dependency injection.
  • a constructor: then the constructor is invoked with the parameters determined by the same algorithm like for the field dependency injection.  

Constructor Injection

@RestController
public class UserController {
    private UserService userService;
 
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
}

Setter Injection

@RestController
public class UserController {
    private UserService userService;
 
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

Field Injection

@RestController
public class UserController {
    @Autowired
    private UserService userService;
}

The following example creates a Spring Boot web application which uses @Autowired annotation.

Project Directory


Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-autowired-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-autowired-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


UserService.java

package com.knf.dev.demo.service;

import org.springframework.stereotype.Component;

@Component
public class UserService {

public String getUser()
{
return "I am the User";
}
}


UserController.java

package com.knf.dev.demo.controller;

import com.knf.dev.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {

@Autowired
UserService userService;

@GetMapping
public String getUser()
{
return userService.getUser();
}
}


Spring Boot Main - Application.java

package com.knf.dev.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}


Run Application - Application.java

Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.
Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:

mvn spring-boot:run


Try to send a GET request to the http://localhost:8080/users endpoint using Postman.
More related topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete