forked from andrei-punko/java-interview-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-review.java
More file actions
130 lines (102 loc) · 2.9 KB
/
Copy pathcode-review.java
File metadata and controls
130 lines (102 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
** Problem Definition:
@Service
@RequiredArgsConstructor
public class CommonService<S extends ExecuteService> {
private final S service;
void process() {
service.execute();
}
}
@Service
@RequiredArgsConstructor
public class InfoService {
private final InfoProcessor service;
void process() {
service.execute();
}
}
@Service
@RequiredArgsConstructor
public class RequestService {
private final RequestProcessor service;
void process() {
service.execute();
}
}
----------------------------------------------------------
** After refactoring:
@Service
@RequiredArgsConstructor
public class CommonService<S extends IExecuteService> {
private final S service;
void process() {
service.execute();
}
}
public interface IExecuteService {
void execute();
}
@Service
@RequiredArgsConstructor
public class InfoService implements IExecuteService {
private final InfoProcessor processor;
@Override
public void execute() {
processor.process();
}
}
@Service
@RequiredArgsConstructor
public class RequestService implements IExecuteService {
private final RequestProcessor processor;
@Override
public void execute() {
processor.process();
}
}
@Service
public class InfoProcessor {
public void execute() {
}
}
@Service
public class RequestProcessor {
public void execute() {
}
}
----------------------------------------------------------
** Logs:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in by.andd3dfx.templateapp.service.review.CommonService required a single bean, but 2 were found:
- infoService: defined in file [C:\Work\Personal\spring-boot-3-template\target\classes\by\andd3dfx\templateapp\service\review\executors\InfoService.class]
- requestService: defined in file [C:\Work\Personal\spring-boot-3-template\target\classes\by\andd3dfx\templateapp\service\review\executors\RequestService.class]
This may be due to missing parameter name information
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
----------------------------------------------------------
** Solution:
@Primary
@Service
@RequiredArgsConstructor
public class InfoService implements IExecuteService {
private final InfoProcessor processor;
@Override
public void execute() {
processor.process();
}
}
or
@Service
public class CommonService<S extends IExecuteService> {
private final S service;
@Autowired
public CommonService(@Qualifier("infoService") S service) {
this.service = service;
}
void process() {
service.execute();
}
}