@@ -88,48 +88,51 @@ void shouldFailWhenControllerAdviceNull() {
88
88
}
89
89
90
90
@ Test
91
- void equalsHashCodeAndToStringForBeanName () {
92
- String beanName = SimpleControllerAdvice .class . getSimpleName ( );
93
- ControllerAdviceBean bean1 = createControllerAdviceBean (SimpleControllerAdvice .class );
94
- ControllerAdviceBean bean2 = createControllerAdviceBean (SimpleControllerAdvice .class );
91
+ void equalsHashCodeAndToString () {
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
98
98
@ Test
99
- void orderedWithLowestPrecedenceByDefaultForBeanName () {
99
+ void orderedWithLowestPrecedenceByDefault () {
100
100
assertOrder (SimpleControllerAdvice .class , Ordered .LOWEST_PRECEDENCE );
101
101
}
102
102
103
103
@ Test
104
- void orderedWithLowestPrecedenceByDefaultForBeanInstance () {
105
- assertOrder (SimpleControllerAdvice .class , Ordered .LOWEST_PRECEDENCE );
106
- }
107
-
108
- @ Test
109
- void orderedViaOrderedInterfaceForBeanName () {
104
+ void orderedViaOrderedInterface () {
110
105
assertOrder (OrderedControllerAdvice .class , 42 );
111
106
}
112
107
113
108
@ Test
114
- void orderedViaOrderedInterfaceForBeanInstance () {
115
- assertOrder (OrderedControllerAdvice .class , 42 );
109
+ void orderedViaAnnotation () {
110
+ assertOrder (OrderAnnotationControllerAdvice .class , 100 );
111
+ assertOrder (PriorityAnnotationControllerAdvice .class , 200 );
116
112
}
117
113
118
114
@ Test
119
- void orderedViaAnnotationForBeanName () {
120
- assertOrder (OrderAnnotationControllerAdvice .class , 100 );
121
- assertOrder (PriorityAnnotationControllerAdvice .class , 200 );
115
+ void resolveBeanForSingletonBean () {
116
+ ControllerAdviceBean cab = createSingletonControllerAdviceBean (SimpleControllerAdvice .class );
117
+ Object bean = this .applicationContext .getBean (getSingletonBeanName (SimpleControllerAdvice .class ));
118
+ assertThat (cab ).extracting ("resolvedBean" ).isNull ();
119
+ Object resolvedBean = cab .resolveBean ();
120
+ assertThat (cab ).extracting ("resolvedBean" ).isEqualTo (bean );
121
+ assertThat (resolvedBean ).isEqualTo (bean );
122
122
}
123
123
124
124
@ Test
125
- void orderedViaAnnotationForBeanInstance () {
126
- assertOrder (OrderAnnotationControllerAdvice .class , 100 );
127
- assertOrder (PriorityAnnotationControllerAdvice .class , 200 );
125
+ void resolveBeanForNonSingletonBean () {
126
+ ControllerAdviceBean cab = createPrototypeControllerAdviceBean (SimpleControllerAdvice .class );
127
+ assertThat (cab ).extracting ("resolvedBean" ).isNull ();
128
+ Object resolvedBean = cab .resolveBean ();
129
+ assertThat (cab ).extracting ("resolvedBean" ).isNull ();
130
+ assertThat (resolvedBean ).isInstanceOf (SimpleControllerAdvice .class );
128
131
}
129
132
130
133
@ Test
131
134
void shouldMatchAll () {
132
- ControllerAdviceBean bean = createControllerAdviceBean (SimpleControllerAdvice .class );
135
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (SimpleControllerAdvice .class );
133
136
assertApplicable ("should match all" , bean , AnnotatedController .class );
134
137
assertApplicable ("should match all" , bean , ImplementationController .class );
135
138
assertApplicable ("should match all" , bean , InheritanceController .class );
@@ -138,7 +141,7 @@ void shouldMatchAll() {
138
141
139
142
@ Test
140
143
void basePackageSupport () {
141
- ControllerAdviceBean bean = createControllerAdviceBean (BasePackageSupport .class );
144
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (BasePackageSupport .class );
142
145
assertApplicable ("base package support" , bean , AnnotatedController .class );
143
146
assertApplicable ("base package support" , bean , ImplementationController .class );
144
147
assertApplicable ("base package support" , bean , InheritanceController .class );
@@ -147,7 +150,7 @@ void basePackageSupport() {
147
150
148
151
@ Test
149
152
void basePackageValueSupport () {
150
- ControllerAdviceBean bean = createControllerAdviceBean (BasePackageValueSupport .class );
153
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (BasePackageValueSupport .class );
151
154
assertApplicable ("base package support" , bean , AnnotatedController .class );
152
155
assertApplicable ("base package support" , bean , ImplementationController .class );
153
156
assertApplicable ("base package support" , bean , InheritanceController .class );
@@ -156,14 +159,14 @@ void basePackageValueSupport() {
156
159
157
160
@ Test
158
161
void annotationSupport () {
159
- ControllerAdviceBean bean = createControllerAdviceBean (AnnotationSupport .class );
162
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (AnnotationSupport .class );
160
163
assertApplicable ("annotation support" , bean , AnnotatedController .class );
161
164
assertNotApplicable ("this bean is not annotated" , bean , InheritanceController .class );
162
165
}
163
166
164
167
@ Test
165
168
void markerClassSupport () {
166
- ControllerAdviceBean bean = createControllerAdviceBean (MarkerClassSupport .class );
169
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (MarkerClassSupport .class );
167
170
assertApplicable ("base package class support" , bean , AnnotatedController .class );
168
171
assertApplicable ("base package class support" , bean , ImplementationController .class );
169
172
assertApplicable ("base package class support" , bean , InheritanceController .class );
@@ -172,7 +175,7 @@ void markerClassSupport() {
172
175
173
176
@ Test
174
177
void shouldNotMatch () {
175
- ControllerAdviceBean bean = createControllerAdviceBean (ShouldNotMatch .class );
178
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (ShouldNotMatch .class );
176
179
assertNotApplicable ("should not match" , bean , AnnotatedController .class );
177
180
assertNotApplicable ("should not match" , bean , ImplementationController .class );
178
181
assertNotApplicable ("should not match" , bean , InheritanceController .class );
@@ -181,7 +184,7 @@ void shouldNotMatch() {
181
184
182
185
@ Test
183
186
void assignableTypesSupport () {
184
- ControllerAdviceBean bean = createControllerAdviceBean (AssignableTypesSupport .class );
187
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (AssignableTypesSupport .class );
185
188
assertApplicable ("controller implements assignable" , bean , ImplementationController .class );
186
189
assertApplicable ("controller inherits assignable" , bean , InheritanceController .class );
187
190
assertNotApplicable ("not assignable" , bean , AnnotatedController .class );
@@ -190,7 +193,7 @@ void assignableTypesSupport() {
190
193
191
194
@ Test
192
195
void multipleMatch () {
193
- ControllerAdviceBean bean = createControllerAdviceBean (MultipleSelectorsSupport .class );
196
+ ControllerAdviceBean bean = createSingletonControllerAdviceBean (MultipleSelectorsSupport .class );
194
197
assertApplicable ("controller implements assignable" , bean , ImplementationController .class );
195
198
assertApplicable ("controller is annotated" , bean , AnnotatedController .class );
196
199
assertNotApplicable ("should not match" , bean , InheritanceController .class );
@@ -216,13 +219,28 @@ public void findAnnotatedBeansSortsBeans() {
216
219
assertThat (adviceBeans ).extracting (ControllerAdviceBean ::getBeanType ).containsExactly (expectedTypes );
217
220
}
218
221
219
- private ControllerAdviceBean createControllerAdviceBean (Class <?> beanType ) {
220
- String beanName = beanType . getSimpleName ( );
222
+ private ControllerAdviceBean createSingletonControllerAdviceBean (Class <?> beanType ) {
223
+ String beanName = getSingletonBeanName ( beanType );
221
224
this .applicationContext .registerSingleton (beanName , beanType );
222
225
ControllerAdvice controllerAdvice = AnnotatedElementUtils .findMergedAnnotation (beanType , ControllerAdvice .class );
223
226
return new ControllerAdviceBean (beanName , this .applicationContext , controllerAdvice );
224
227
}
225
228
229
+ private static String getSingletonBeanName (Class <?> beanType ) {
230
+ return beanType .getSimpleName () + "Singleton" ;
231
+ }
232
+
233
+ private ControllerAdviceBean createPrototypeControllerAdviceBean (Class <?> beanType ) {
234
+ String beanName = getPrototypeBeanName (beanType );
235
+ this .applicationContext .registerPrototype (beanName , beanType );
236
+ ControllerAdvice controllerAdvice = AnnotatedElementUtils .findMergedAnnotation (beanType , ControllerAdvice .class );
237
+ return new ControllerAdviceBean (beanName , this .applicationContext , controllerAdvice );
238
+ }
239
+
240
+ private static String getPrototypeBeanName (Class <?> beanType ) {
241
+ return beanType .getSimpleName () + "Prototype" ;
242
+ }
243
+
226
244
private void assertEqualsHashCodeAndToString (ControllerAdviceBean bean1 , ControllerAdviceBean bean2 , String toString ) {
227
245
assertThat (bean1 ).isEqualTo (bean2 );
228
246
assertThat (bean2 ).isEqualTo (bean1 );
@@ -232,7 +250,7 @@ private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, Control
232
250
}
233
251
234
252
private void assertOrder (Class <?> beanType , int expectedOrder ) {
235
- assertThat (createControllerAdviceBean (beanType ).getOrder ()).isEqualTo (expectedOrder );
253
+ assertThat (createSingletonControllerAdviceBean (beanType ).getOrder ()).isEqualTo (expectedOrder );
236
254
}
237
255
238
256
private void assertApplicable (String message , ControllerAdviceBean controllerAdvice , Class <?> controllerBeanType ) {
0 commit comments