JAVA-19544 Move Spring @Primary Annotation article from spring-core-2 module to spring-boot-annotations-2 module (#13687)

This commit is contained in:
anuragkumawat
2023-03-22 21:28:27 +05:30
committed by GitHub
parent 1f20f35689
commit 0dcb9529a7
9 changed files with 1 additions and 1 deletions

View File

@@ -6,4 +6,5 @@ This module contains articles about Spring Boot annotations
- [Spring Conditional Annotations](https://www.baeldung.com/spring-conditional-annotations)
- [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation)
- [Spring @Primary Annotation](http://www.baeldung.com/spring-primary)
- More articles: [[<-- prev]](/spring-boot-modules/spring-boot-annotations)

View File

@@ -0,0 +1,22 @@
package com.baeldung.primary;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
@ComponentScan(basePackages="com.baeldung.primary")
public class Config {
@Bean
public Employee johnEmployee(){
return new Employee("John");
}
@Bean
@Primary
public Employee tonyEmployee(){
return new Employee("Tony");
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.primary;
import org.springframework.stereotype.Component;
@Component
public class DepartmentManager implements Manager {
@Override
public String getManagerName() {
return "Department manager";
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.primary;
/**
* Created by Gebruiker on 7/17/2018.
*/
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
'}';
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.primary;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class GeneralManager implements Manager {
@Override
public String getManagerName() {
return "General manager";
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.primary;
/**
* Created by Gebruiker on 7/19/2018.
*/
public interface Manager {
String getManagerName();
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.primary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Gebruiker on 7/19/2018.
*/@Service
public class ManagerService {
@Autowired
private Manager manager;
public Manager getManager() {
return manager;
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.primary;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class PrimaryApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(Config.class);
Employee employee = context.getBean(Employee.class);
System.out.println(employee);
ManagerService service = context.getBean(ManagerService.class);
Manager manager = service.getManager();
System.out.println(manager.getManagerName());
}
}