Skip to content

Commit 70b0b97

Browse files
committed
Fix cyclical package dependency
1 parent 6e85dd8 commit 70b0b97

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

spring-web/src/main/java/org/springframework/web/bind/support/ControllerAdviceBean.java renamed to spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.web.bind.support;
16+
package org.springframework.web.method;
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
@@ -29,7 +29,11 @@
2929

3030
/**
3131
* Encapsulates information about an {@linkplain ControllerAdvice @ControllerAdvice}
32-
* bean without requiring the bean to be instantiated.
32+
* Spring-managed bean without necessarily requiring it to be instantiated.
33+
*
34+
* <p>The {@link #findAnnotatedBeans(ApplicationContext)} method can be used to discover
35+
* such beans. However, an {@code ControllerAdviceBean} may be created from
36+
* any object, including ones without an {@code @ControllerAdvice}.
3337
*
3438
* @author Rossen Stoyanchev
3539
* @since 3.2
@@ -55,7 +59,12 @@ public ControllerAdviceBean(String beanName, BeanFactory beanFactory) {
5559
"Bean factory [" + beanFactory + "] does not contain bean " + "with name [" + beanName + "]");
5660
this.bean = beanName;
5761
this.beanFactory = beanFactory;
58-
this.order = initOrder(this.beanFactory.getType(beanName));
62+
this.order = initOrderFromBeanType(this.beanFactory.getType(beanName));
63+
}
64+
65+
private static int initOrderFromBeanType(Class<?> beanType) {
66+
Order annot = AnnotationUtils.findAnnotation(beanType, Order.class);
67+
return (annot != null) ? annot.value() : Ordered.LOWEST_PRECEDENCE;
5968
}
6069

6170
/**
@@ -65,21 +74,20 @@ public ControllerAdviceBean(String beanName, BeanFactory beanFactory) {
6574
public ControllerAdviceBean(Object bean) {
6675
Assert.notNull(bean, "'bean' must not be null");
6776
this.bean = bean;
68-
this.order = initOrder(bean.getClass());
77+
this.order = initOrderFromBean(bean);
6978
this.beanFactory = null;
7079
}
7180

72-
private static int initOrder(Class<?> beanType) {
73-
Order orderAnnot = AnnotationUtils.findAnnotation(beanType, Order.class);
74-
return (orderAnnot != null) ? orderAnnot.value() : Ordered.LOWEST_PRECEDENCE;
81+
private static int initOrderFromBean(Object bean) {
82+
return (bean instanceof Ordered) ? ((Ordered) bean).getOrder() : initOrderFromBeanType(bean.getClass());
7583
}
7684

7785
/**
7886
* Find the names of beans annotated with
7987
* {@linkplain ControllerAdvice @ControllerAdvice} in the given
8088
* ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
8189
*/
82-
public static List<ControllerAdviceBean> findBeans(ApplicationContext applicationContext) {
90+
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {
8391
List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();
8492
for (String name : applicationContext.getBeanDefinitionNames()) {
8593
if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
@@ -90,12 +98,9 @@ public static List<ControllerAdviceBean> findBeans(ApplicationContext applicatio
9098
}
9199

92100
/**
93-
* Return a bean instance if necessary resolving the bean name through the BeanFactory.
101+
* Returns the order value extracted from the {@link ControllerAdvice}
102+
* annotation or {@link Ordered#LOWEST_PRECEDENCE} otherwise.
94103
*/
95-
public Object resolveBean() {
96-
return (this.bean instanceof String) ? this.beanFactory.getBean((String) this.bean) : this.bean;
97-
}
98-
99104
public int getOrder() {
100105
return this.order;
101106
}
@@ -111,6 +116,13 @@ public Class<?> getBeanType() {
111116
return ClassUtils.getUserClass(clazz);
112117
}
113118

119+
/**
120+
* Return a bean instance if necessary resolving the bean name through the BeanFactory.
121+
*/
122+
public Object resolveBean() {
123+
return (this.bean instanceof String) ? this.beanFactory.getBean((String) this.bean) : this.bean;
124+
}
125+
114126
@Override
115127
public boolean equals(Object o) {
116128
if (this == o) {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
import org.springframework.util.ClassUtils;
3030

3131
/**
32-
* Encapsulates information about a bean method consisting of a {@linkplain #getMethod() method} and a
33-
* {@linkplain #getBean() bean}. Provides convenient access to method parameters, the method return value,
34-
* method annotations.
32+
* Encapsulates information about a bean method consisting of a
33+
* {@linkplain #getMethod() method} and a {@linkplain #getBean() bean}. Provides
34+
* convenient access to method parameters, the method return value, method
35+
* annotations.
3536
*
36-
* <p>The class may be created with a bean instance or with a bean name (e.g. lazy bean, prototype bean).
37-
* Use {@link #createWithResolvedBean()} to obtain an {@link HandlerMethod} instance with a bean instance
38-
* initialized through the bean factory.
37+
* <p>The class may be created with a bean instance or with a bean name (e.g. lazy
38+
* bean, prototype bean). Use {@link #createWithResolvedBean()} to obtain an
39+
* {@link HandlerMethod} instance with a bean instance initialized through the
40+
* bean factory.
3941
*
4042
* @author Arjen Poutsma
4143
* @author Rossen Stoyanchev

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter;
4242
import org.springframework.web.accept.ContentNegotiationManager;
4343
import org.springframework.web.bind.annotation.ControllerAdvice;
44-
import org.springframework.web.bind.support.ControllerAdviceBean;
4544
import org.springframework.web.context.request.ServletWebRequest;
45+
import org.springframework.web.method.ControllerAdviceBean;
4646
import org.springframework.web.method.HandlerMethod;
4747
import org.springframework.web.method.annotation.ExceptionHandlerMethodResolver;
4848
import org.springframework.web.method.annotation.MapMethodProcessor;
@@ -285,7 +285,7 @@ private void initExceptionHandlerAdviceCache() {
285285
logger.debug("Looking for exception mappings: " + getApplicationContext());
286286
}
287287

288-
List<ControllerAdviceBean> beans = ControllerAdviceBean.findBeans(getApplicationContext());
288+
List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
289289
Collections.sort(beans, new OrderComparator());
290290

291291
for (ControllerAdviceBean bean : beans) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import org.springframework.web.bind.annotation.InitBinder;
5555
import org.springframework.web.bind.annotation.ModelAttribute;
5656
import org.springframework.web.bind.annotation.RequestMapping;
57-
import org.springframework.web.bind.support.ControllerAdviceBean;
5857
import org.springframework.web.bind.support.DefaultDataBinderFactory;
5958
import org.springframework.web.bind.support.DefaultSessionAttributeStore;
6059
import org.springframework.web.bind.support.SessionAttributeStore;
@@ -67,6 +66,7 @@
6766
import org.springframework.web.context.request.async.AsyncWebRequest;
6867
import org.springframework.web.context.request.async.AsyncWebUtils;
6968
import org.springframework.web.context.request.async.WebAsyncManager;
69+
import org.springframework.web.method.ControllerAdviceBean;
7070
import org.springframework.web.method.HandlerMethod;
7171
import org.springframework.web.method.HandlerMethodSelector;
7272
import org.springframework.web.method.annotation.ErrorsMethodArgumentResolver;
@@ -592,7 +592,7 @@ private void initControllerAdviceCache() {
592592
logger.debug("Looking for controller advice: " + getApplicationContext());
593593
}
594594

595-
List<ControllerAdviceBean> beans = ControllerAdviceBean.findBeans(getApplicationContext());
595+
List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
596596
Collections.sort(beans, new OrderComparator());
597597

598598
for (ControllerAdviceBean bean : beans) {

0 commit comments

Comments
 (0)