@@ -89,9 +89,9 @@ void shouldFailWhenControllerAdviceNull() {
89
89
90
90
@ Test
91
91
void equalsHashCodeAndToStringForBeanName () {
92
- String beanName = SimpleControllerAdvice .class . getSimpleName ( );
93
- ControllerAdviceBean bean1 = createControllerAdviceBean (SimpleControllerAdvice .class );
94
- ControllerAdviceBean bean2 = createControllerAdviceBean (SimpleControllerAdvice .class );
92
+ String beanName = getSingletonBeanName ( SimpleControllerAdvice .class );
93
+ ControllerAdviceBean bean1 = createSingletonControllerAdviceBean (SimpleControllerAdvice .class );
94
+ ControllerAdviceBean bean2 = createSingletonControllerAdviceBean (SimpleControllerAdvice .class );
95
95
assertEqualsHashCodeAndToString (bean1 , bean2 , beanName );
96
96
}
97
97
@@ -127,9 +127,28 @@ void orderedViaAnnotationForBeanInstance() {
127
127
assertOrder (PriorityAnnotationControllerAdvice .class , 200 );
128
128
}
129
129
130
+ @ Test
131
+ void resolveBeanForSingletonBean () {
132
+ ControllerAdviceBean cab = createSingletonControllerAdviceBean (SimpleControllerAdvice .class );
133
+ Object bean = this .applicationContext .getBean (getSingletonBeanName (SimpleControllerAdvice .class ));
134
+ assertThat (cab ).extracting ("resolvedBean" ).isNull ();
135
+ Object resolvedBean = cab .resolveBean ();
136
+ assertThat (cab ).extracting ("resolvedBean" ).isEqualTo (bean );
137
+ assertThat (resolvedBean ).isEqualTo (bean );
138
+ }
139
+
140
+ @ Test
141
+ void resolveBeanForNonSingletonBean () {
142
+ ControllerAdviceBean cab = createPrototypeControllerAdviceBean (SimpleControllerAdvice .class );
143
+ assertThat (cab ).extracting ("resolvedBean" ).isNull ();
144
+ Object resolvedBean = cab .resolveBean ();
145
+ assertThat (cab ).extracting ("resolvedBean" ).isNull ();
146
+ assertThat (resolvedBean ).isInstanceOf (SimpleControllerAdvice .class );
147
+ }
148
+
130
149
@ Test
131
150
void shouldMatchAll () {
132
- ControllerAdviceBean bean = createControllerAdviceBean (SimpleControllerAdvice .class );
151
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (SimpleControllerAdvice .class );
133
152
assertApplicable ("should match all" , bean , AnnotatedController .class );
134
153
assertApplicable ("should match all" , bean , ImplementationController .class );
135
154
assertApplicable ("should match all" , bean , InheritanceController .class );
@@ -138,7 +157,7 @@ void shouldMatchAll() {
138
157
139
158
@ Test
140
159
void basePackageSupport () {
141
- ControllerAdviceBean bean = createControllerAdviceBean (BasePackageSupport .class );
160
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (BasePackageSupport .class );
142
161
assertApplicable ("base package support" , bean , AnnotatedController .class );
143
162
assertApplicable ("base package support" , bean , ImplementationController .class );
144
163
assertApplicable ("base package support" , bean , InheritanceController .class );
@@ -147,7 +166,7 @@ void basePackageSupport() {
147
166
148
167
@ Test
149
168
void basePackageValueSupport () {
150
- ControllerAdviceBean bean = createControllerAdviceBean (BasePackageValueSupport .class );
169
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (BasePackageValueSupport .class );
151
170
assertApplicable ("base package support" , bean , AnnotatedController .class );
152
171
assertApplicable ("base package support" , bean , ImplementationController .class );
153
172
assertApplicable ("base package support" , bean , InheritanceController .class );
@@ -156,14 +175,14 @@ void basePackageValueSupport() {
156
175
157
176
@ Test
158
177
void annotationSupport () {
159
- ControllerAdviceBean bean = createControllerAdviceBean (AnnotationSupport .class );
178
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (AnnotationSupport .class );
160
179
assertApplicable ("annotation support" , bean , AnnotatedController .class );
161
180
assertNotApplicable ("this bean is not annotated" , bean , InheritanceController .class );
162
181
}
163
182
164
183
@ Test
165
184
void markerClassSupport () {
166
- ControllerAdviceBean bean = createControllerAdviceBean (MarkerClassSupport .class );
185
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (MarkerClassSupport .class );
167
186
assertApplicable ("base package class support" , bean , AnnotatedController .class );
168
187
assertApplicable ("base package class support" , bean , ImplementationController .class );
169
188
assertApplicable ("base package class support" , bean , InheritanceController .class );
@@ -172,7 +191,7 @@ void markerClassSupport() {
172
191
173
192
@ Test
174
193
void shouldNotMatch () {
175
- ControllerAdviceBean bean = createControllerAdviceBean (ShouldNotMatch .class );
194
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (ShouldNotMatch .class );
176
195
assertNotApplicable ("should not match" , bean , AnnotatedController .class );
177
196
assertNotApplicable ("should not match" , bean , ImplementationController .class );
178
197
assertNotApplicable ("should not match" , bean , InheritanceController .class );
@@ -181,7 +200,7 @@ void shouldNotMatch() {
181
200
182
201
@ Test
183
202
void assignableTypesSupport () {
184
- ControllerAdviceBean bean = createControllerAdviceBean (AssignableTypesSupport .class );
203
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (AssignableTypesSupport .class );
185
204
assertApplicable ("controller implements assignable" , bean , ImplementationController .class );
186
205
assertApplicable ("controller inherits assignable" , bean , InheritanceController .class );
187
206
assertNotApplicable ("not assignable" , bean , AnnotatedController .class );
@@ -190,7 +209,7 @@ void assignableTypesSupport() {
190
209
191
210
@ Test
192
211
void multipleMatch () {
193
- ControllerAdviceBean bean = createControllerAdviceBean (MultipleSelectorsSupport .class );
212
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (MultipleSelectorsSupport .class );
194
213
assertApplicable ("controller implements assignable" , bean , ImplementationController .class );
195
214
assertApplicable ("controller is annotated" , bean , AnnotatedController .class );
196
215
assertNotApplicable ("should not match" , bean , InheritanceController .class );
@@ -216,13 +235,28 @@ public void findAnnotatedBeansSortsBeans() {
216
235
assertThat (adviceBeans ).extracting (ControllerAdviceBean ::getBeanType ).containsExactly (expectedTypes );
217
236
}
218
237
219
- private ControllerAdviceBean createControllerAdviceBean (Class <?> beanType ) {
220
- String beanName = beanType . getSimpleName ( );
238
+ private ControllerAdviceBean createSingletonControllerAdviceBean (Class <?> beanType ) {
239
+ String beanName = getSingletonBeanName ( beanType );
221
240
this .applicationContext .registerSingleton (beanName , beanType );
222
241
ControllerAdvice controllerAdvice = AnnotatedElementUtils .findMergedAnnotation (beanType , ControllerAdvice .class );
223
242
return new ControllerAdviceBean (beanName , this .applicationContext , controllerAdvice );
224
243
}
225
244
245
+ private static String getSingletonBeanName (Class <?> beanType ) {
246
+ return beanType .getSimpleName () + "Singleton" ;
247
+ }
248
+
249
+ private ControllerAdviceBean createPrototypeControllerAdviceBean (Class <?> beanType ) {
250
+ String beanName = getPrototypeBeanName (beanType );
251
+ this .applicationContext .registerPrototype (beanName , beanType );
252
+ ControllerAdvice controllerAdvice = AnnotatedElementUtils .findMergedAnnotation (beanType , ControllerAdvice .class );
253
+ return new ControllerAdviceBean (beanName , this .applicationContext , controllerAdvice );
254
+ }
255
+
256
+ private static String getPrototypeBeanName (Class <?> beanType ) {
257
+ return beanType .getSimpleName () + "Prototype" ;
258
+ }
259
+
226
260
private void assertEqualsHashCodeAndToString (ControllerAdviceBean bean1 , ControllerAdviceBean bean2 , String toString ) {
227
261
assertThat (bean1 ).isEqualTo (bean2 );
228
262
assertThat (bean2 ).isEqualTo (bean1 );
@@ -232,7 +266,7 @@ private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, Control
232
266
}
233
267
234
268
private void assertOrder (Class <?> beanType , int expectedOrder ) {
235
- assertThat (createControllerAdviceBean (beanType ).getOrder ()).isEqualTo (expectedOrder );
269
+ assertThat (createSingletonControllerAdviceBean (beanType ).getOrder ()).isEqualTo (expectedOrder );
236
270
}
237
271
238
272
private void assertApplicable (String message , ControllerAdviceBean controllerAdvice , Class <?> controllerBeanType ) {
0 commit comments