JAVA-19544 Move Spring @Primary Annotation article from spring-core-2 module to spring-boot-annotations-2 module (#13687)
This commit is contained in:
@@ -8,7 +8,6 @@ This module contains articles about core Spring functionality
|
||||
- [Spring Profiles](http://www.baeldung.com/spring-profiles)
|
||||
- [Quick Guide to Spring Bean Scopes](http://www.baeldung.com/spring-bean-scopes)
|
||||
- [@Order in Spring](http://www.baeldung.com/spring-order)
|
||||
- [Spring @Primary Annotation](http://www.baeldung.com/spring-primary)
|
||||
- [Spring Events](https://www.baeldung.com/spring-events)
|
||||
- [Spring Null-Safety Annotations](https://www.baeldung.com/spring-null-safety-annotations)
|
||||
- More articles: [[<-- prev]](/spring-core)[[next -->]](/spring-core-3)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.primary;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DepartmentManager implements Manager {
|
||||
@Override
|
||||
public String getManagerName() {
|
||||
return "Department manager";
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.primary;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 7/19/2018.
|
||||
*/
|
||||
public interface Manager {
|
||||
String getManagerName();
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user