Step 1:
Define beans by
providing some names to them.
@Configuration public class EmployeeConfiguration { @Bean("krishna") public Employee newEmployee1() { System.out.println("New bean is getting initialized"); Employee emp = new Employee(); emp.setId(1); emp.setFirstName("Krishna"); emp.setLastName("Majety"); return emp; } @Bean("ram") public Employee newEmployee2() { System.out.println("New bean is getting initialized"); Employee emp = new Employee(); emp.setId(1); emp.setFirstName("Ram"); emp.setLastName("Gurram"); return emp; } }
Step 2:
Access specific beans
via @Qualifier annotation.
@Autowired @Qualifier("krishna") private Employee emp1; @Autowired @Qualifier("ram") private Employee emp2;
Find the
below working example.
Employee.java
package com.sample.app.model; public class Employee { private int id; private String firstName; private String lastName; public Employee() { System.out.println("Constructor Called"); } 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; } public void print() { StringBuilder builder = new StringBuilder(); builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=") .append(lastName).append("]"); System.out.println(builder.toString()); } }
EmployeeConfiguration.java
package com.sample.app.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.sample.app.model.Employee; @Configuration public class EmployeeConfiguration { @Bean("krishna") public Employee newEmployee1() { System.out.println("New bean is getting initialized"); Employee emp = new Employee(); emp.setId(1); emp.setFirstName("Krishna"); emp.setLastName("Majety"); return emp; } @Bean("ram") public Employee newEmployee2() { System.out.println("New bean is getting initialized"); Employee emp = new Employee(); emp.setId(1); emp.setFirstName("Ram"); emp.setLastName("Gurram"); return emp; } }
application.properties
logging.level.root=WARN
App.java
package com.sample.app; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import com.sample.app.model.Employee; @SpringBootApplication public class App { @Autowired @Qualifier("krishna") private Employee emp1; @Autowired @Qualifier("ram") private Employee emp2; public static void main(String args[]) { System.out.println("Main Application running......."); SpringApplication.run(App.class, args); System.out.println("ApplicationContext initialized"); } @Bean public CommandLineRunner demo() { return (args) -> { System.out.println("Inside command line runner"); emp1.print(); emp2.print(); }; } }
Total
project structure looks like below.
Run
App.java, you can see below messages in console.
Main Application running....... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) New bean is getting initialized Constructor Called New bean is getting initialized Constructor Called Inside command line runner Employee [id=1, firstName=Krishna, lastName=Majety] Employee [id=1, firstName=Ram, lastName=Gurram] ApplicationContext initialized
You can
download complete working application from this link.
No comments:
Post a Comment