Friday, 20 July 2018

Spring boot: Autowire spring bean by name

By combining @Autowired and @Qualifier annotations, you can autowire a bean by name.

Synatx
@Autowired
@Qualifier("beanname")

Example
@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