Friday 20 July 2018

Spring boot: @Autowired annotation example

Auto wiring is a technique, where spring automatically inject a bean by using type, name (or) constructor arguments. Spring supports 4 modes of auto wiring.

         1. No Auto wiring
         2. Auto wiring by name
         3. Auto wiring by type
         4. Auto wiring by constructor.

I explained above modes in my previous tutorials.

In this post, I am going to show, how can you use @Autowired annotation to inject beans.

Define a bean in any configuration class.
         @Bean(autowire = Autowire.BY_NAME, name = "defaultEmployee")
         public Employee getDefaultEmployee() {
                  Employee emp = new Employee();
                  emp.setFirstName("NO_NAME");
                  emp.setId(-1);
                  emp.setLastName("NO_NAME");
                  return emp;
         }

As you see, I given a name 'defaultEmployee' to the bean.

Use @Autowired and @Qualifier annotations to inject the bean.
        
@Autowired
@Qualifier("defaultEmployee")
private Employee defaultEmployee;

Find the below working application.

Employee.java
package com.sample.myApp.model;

import org.springframework.stereotype.Component;

@Component
public class Employee {

 private int id;
 private String firstName;
 private String lastName;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

}

EmployeeConfiguration.java
package com.sample.myApp.Configuration;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sample.myApp.model.Employee;

@Configuration
public class EmployeeConfiguration {

 @Bean(autowire = Autowire.BY_NAME, name = "defaultEmployee")
 public Employee getDefaultEmployee() {
  Employee emp = new Employee();
  emp.setFirstName("NO_NAME");
  emp.setId(-1);
  emp.setLastName("NO_NAME");
  return emp;
 }

 @Bean(autowire = Autowire.BY_NAME, name = "genericEmployee")
 public Employee getGenericEmployee() {
  Employee emp = new Employee();
  emp.setFirstName("krishna");
  emp.setId(1);
  emp.setLastName("Gurram");
  return emp;
 }
}

EmployeeUtil.java
package com.sample.myApp.util;

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

import com.sample.myApp.model.Employee;

@Component
public class EmployeeUtil {

 @Autowired
 @Qualifier("defaultEmployee")
 private Employee defaultEmployee;

 @Autowired
 @Qualifier("genericEmployee")
 private Employee genericEmployee;

 public Employee getDefaultEmployee() {
  return defaultEmployee;
 }

 public Employee getGenericEmployee() {
  return genericEmployee;
 }

}

Application.java
package com.sample.myApp;

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

import com.sample.myApp.util.EmployeeUtil;

@SpringBootApplication
public class Application {

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

  EmployeeUtil empUtil = applicationContext.getBean(EmployeeUtil.class);

  System.out.println(empUtil.getDefaultEmployee());
  System.out.println(empUtil.getGenericEmployee());

  applicationContext.close();
 }
}


Output
Employee [id=-1, firstName=NO_NAME, lastName=NO_NAME]
Employee [id=1, firstName=krishna, lastName=Gurram]






Previous                                                 Next                                                 Home

No comments:

Post a Comment