Skip to content

Commit 189e626

Browse files
MherBaghinyanpivovarit
authored andcommitted
Bael 1832 (#4748)
* @primary annotation * @primary annotation Employee name * Update PrimaryApplication.java * @primary annotation with @component
1 parent f431a4c commit 189e626

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.primary;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Primary;
7+
8+
@Configuration
9+
@ComponentScan(basePackages="org.baeldung.primary")
10+
public class Config {
11+
12+
@Bean
13+
public Employee JohnEmployee(){
14+
return new Employee("John");
15+
}
16+
17+
@Bean
18+
@Primary
19+
public Employee TonyEmployee(){
20+
return new Employee("Tony");
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.primary;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class DepartmentManager implements Manager {
7+
@Override
8+
public String getManagerName() {
9+
return "Department manager";
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.primary;
2+
3+
/**
4+
* Created by Gebruiker on 7/17/2018.
5+
*/
6+
public class Employee {
7+
8+
private String name;
9+
10+
public Employee(String name) {
11+
this.name = name;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return "Employee{" +
17+
"name='" + name + '\'' +
18+
'}';
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.primary;
2+
3+
import org.springframework.context.annotation.Primary;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Primary
8+
public class GeneralManager implements Manager {
9+
10+
@Override
11+
public String getManagerName() {
12+
return "General manager";
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung.primary;
2+
3+
/**
4+
* Created by Gebruiker on 7/19/2018.
5+
*/
6+
public interface Manager {
7+
String getManagerName();
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.primary;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
/**
7+
* Created by Gebruiker on 7/19/2018.
8+
*/@Service
9+
public class ManagerService {
10+
11+
@Autowired
12+
private Manager manager;
13+
14+
public Manager getManager() {
15+
return manager;
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.primary;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
6+
public class PrimaryApplication {
7+
8+
public static void main(String[] args) {
9+
AnnotationConfigApplicationContext context
10+
= new AnnotationConfigApplicationContext(Config.class);
11+
12+
Employee employee = context.getBean(Employee.class);
13+
System.out.println(employee);
14+
15+
ManagerService service = context.getBean(ManagerService.class);
16+
Manager manager = service.getManager();
17+
System.out.println(manager.getManagerName());
18+
}
19+
20+
}

0 commit comments

Comments
 (0)