File tree Expand file tree Collapse file tree 7 files changed +112
-0
lines changed
spring-all/src/main/java/org/baeldung/primary Expand file tree Collapse file tree 7 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .primary ;
2+
3+ /**
4+ * Created by Gebruiker on 7/19/2018.
5+ */
6+ public interface Manager {
7+ String getManagerName ();
8+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments