Skip to content

Commit 4568edf

Browse files
committed
Simplify ControllerAdviceBean constructors
See gh-23163
1 parent 8f30639 commit 4568edf

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed

spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@
4747
public class ControllerAdviceBean implements Ordered {
4848

4949
/**
50-
* Declared as {@code Object} since this may be a reference to a
51-
* {@code String} representing the bean name or a reference to the actual
52-
* bean instance.
50+
* Reference to the actual bean instance or a {@code String} representing
51+
* the bean name.
5352
*/
54-
private final Object bean;
53+
private final Object beanOrName;
5554

5655
/**
57-
* A reference to the resolved bean instance, potentially lazily retrieved
56+
* Reference to the resolved bean instance, potentially lazily retrieved
5857
* via the {@code BeanFactory}.
5958
*/
6059
private Object resolvedBean;
@@ -75,7 +74,13 @@ public class ControllerAdviceBean implements Ordered {
7574
* @param bean the bean instance
7675
*/
7776
public ControllerAdviceBean(Object bean) {
78-
this(bean, null);
77+
Assert.notNull(bean, "Bean must not be null");
78+
this.beanOrName = bean;
79+
this.resolvedBean = bean;
80+
this.beanType = ClassUtils.getUserClass(bean.getClass());
81+
this.beanTypePredicate = createBeanTypePredicate(this.beanType);
82+
this.beanFactory = null;
83+
this.order = initOrderFromBean(bean);
7984
}
8085

8186
/**
@@ -85,51 +90,16 @@ public ControllerAdviceBean(Object bean) {
8590
* and later to resolve the actual bean
8691
*/
8792
public ControllerAdviceBean(String beanName, BeanFactory beanFactory) {
88-
this((Object) beanName, beanFactory);
89-
}
90-
91-
private ControllerAdviceBean(Object bean, @Nullable BeanFactory beanFactory) {
92-
this.bean = bean;
93+
Assert.hasText(beanName, "Bean name must contain text");
94+
Assert.notNull(beanFactory, "BeanFactory must not be null");
95+
Assert.isTrue(beanFactory.containsBean(beanName), () -> "BeanFactory [" + beanFactory
96+
+ "] does not contain specified controller advice bean '" + beanName + "'");
97+
98+
this.beanOrName = beanName;
99+
this.beanType = getBeanType(beanName, beanFactory);
100+
this.beanTypePredicate = createBeanTypePredicate(this.beanType);
93101
this.beanFactory = beanFactory;
94-
Class<?> beanType;
95-
96-
if (bean instanceof String) {
97-
String beanName = (String) bean;
98-
Assert.hasText(beanName, "Bean name must not be empty");
99-
Assert.notNull(beanFactory, "BeanFactory must not be null");
100-
if (!beanFactory.containsBean(beanName)) {
101-
throw new IllegalArgumentException("BeanFactory [" + beanFactory +
102-
"] does not contain specified controller advice bean '" + beanName + "'");
103-
}
104-
beanType = this.beanFactory.getType(beanName);
105-
if (beanType != null) {
106-
beanType = ClassUtils.getUserClass(beanType);
107-
}
108-
this.order = initOrderFromBeanType(beanType);
109-
}
110-
else {
111-
Assert.notNull(bean, "Bean must not be null");
112-
beanType = ClassUtils.getUserClass(bean.getClass());
113-
this.resolvedBean = bean;
114-
this.order = initOrderFromBean(bean);
115-
}
116-
117-
this.beanType = beanType;
118-
119-
ControllerAdvice annotation = (beanType != null ?
120-
AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class) : null);
121-
122-
if (annotation != null) {
123-
this.beanTypePredicate = HandlerTypePredicate.builder()
124-
.basePackage(annotation.basePackages())
125-
.basePackageClass(annotation.basePackageClasses())
126-
.assignableType(annotation.assignableTypes())
127-
.annotation(annotation.annotations())
128-
.build();
129-
}
130-
else {
131-
this.beanTypePredicate = HandlerTypePredicate.forAnyHandlerType();
132-
}
102+
this.order = initOrderFromBeanType(this.beanType);
133103
}
134104

135105

@@ -160,9 +130,9 @@ public Class<?> getBeanType() {
160130
*/
161131
public Object resolveBean() {
162132
if (this.resolvedBean == null) {
163-
// this.bean must be a String representing the bean name if
133+
// this.beanOrName must be a String representing the bean name if
164134
// this.resolvedBean is null.
165-
this.resolvedBean = obtainBeanFactory().getBean((String) this.bean);
135+
this.resolvedBean = obtainBeanFactory().getBean((String) this.beanOrName);
166136
}
167137
return this.resolvedBean;
168138
}
@@ -193,17 +163,17 @@ public boolean equals(@Nullable Object other) {
193163
return false;
194164
}
195165
ControllerAdviceBean otherAdvice = (ControllerAdviceBean) other;
196-
return (this.bean.equals(otherAdvice.bean) && this.beanFactory == otherAdvice.beanFactory);
166+
return (this.beanOrName.equals(otherAdvice.beanOrName) && this.beanFactory == otherAdvice.beanFactory);
197167
}
198168

199169
@Override
200170
public int hashCode() {
201-
return this.bean.hashCode();
171+
return this.beanOrName.hashCode();
202172
}
203173

204174
@Override
205175
public String toString() {
206-
return this.bean.toString();
176+
return this.beanOrName.toString();
207177
}
208178

209179

@@ -222,6 +192,26 @@ public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext c
222192
return adviceBeans;
223193
}
224194

195+
private static Class<?> getBeanType(String beanName, BeanFactory beanFactory) {
196+
Class<?> beanType = beanFactory.getType(beanName);
197+
return (beanType != null ? ClassUtils.getUserClass(beanType) : null);
198+
}
199+
200+
private static HandlerTypePredicate createBeanTypePredicate(Class<?> beanType) {
201+
ControllerAdvice annotation = (beanType != null ?
202+
AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class) : null);
203+
204+
if (annotation != null) {
205+
return HandlerTypePredicate.builder()
206+
.basePackage(annotation.basePackages())
207+
.basePackageClass(annotation.basePackageClasses())
208+
.assignableType(annotation.assignableTypes())
209+
.annotation(annotation.annotations())
210+
.build();
211+
}
212+
return HandlerTypePredicate.forAnyHandlerType();
213+
}
214+
225215
private static int initOrderFromBean(Object bean) {
226216
return (bean instanceof Ordered ? ((Ordered) bean).getOrder() : initOrderFromBeanType(bean.getClass()));
227217
}

spring-web/src/test/java/org/springframework/web/method/ControllerAdviceBeanTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public void constructorPreconditions() {
5151

5252
assertThatIllegalArgumentException()
5353
.isThrownBy(() -> new ControllerAdviceBean((String) null, null))
54-
.withMessage("Bean must not be null");
54+
.withMessage("Bean name must contain text");
5555

5656
assertThatIllegalArgumentException()
5757
.isThrownBy(() -> new ControllerAdviceBean("", null))
58-
.withMessage("Bean name must not be empty");
58+
.withMessage("Bean name must contain text");
5959

6060
assertThatIllegalArgumentException()
6161
.isThrownBy(() -> new ControllerAdviceBean("\t", null))
62-
.withMessage("Bean name must not be empty");
62+
.withMessage("Bean name must contain text");
6363

6464
assertThatIllegalArgumentException()
6565
.isThrownBy(() -> new ControllerAdviceBean("myBean", null))

0 commit comments

Comments
 (0)