13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .springframework .web .bind . support ;
16
+ package org .springframework .web .method ;
17
17
18
18
import java .util .ArrayList ;
19
19
import java .util .List ;
29
29
30
30
/**
31
31
* 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}.
33
37
*
34
38
* @author Rossen Stoyanchev
35
39
* @since 3.2
@@ -55,7 +59,12 @@ public ControllerAdviceBean(String beanName, BeanFactory beanFactory) {
55
59
"Bean factory [" + beanFactory + "] does not contain bean " + "with name [" + beanName + "]" );
56
60
this .bean = beanName ;
57
61
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 ;
59
68
}
60
69
61
70
/**
@@ -65,21 +74,20 @@ public ControllerAdviceBean(String beanName, BeanFactory beanFactory) {
65
74
public ControllerAdviceBean (Object bean ) {
66
75
Assert .notNull (bean , "'bean' must not be null" );
67
76
this .bean = bean ;
68
- this .order = initOrder (bean . getClass () );
77
+ this .order = initOrderFromBean (bean );
69
78
this .beanFactory = null ;
70
79
}
71
80
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 ());
75
83
}
76
84
77
85
/**
78
86
* Find the names of beans annotated with
79
87
* {@linkplain ControllerAdvice @ControllerAdvice} in the given
80
88
* ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
81
89
*/
82
- public static List <ControllerAdviceBean > findBeans (ApplicationContext applicationContext ) {
90
+ public static List <ControllerAdviceBean > findAnnotatedBeans (ApplicationContext applicationContext ) {
83
91
List <ControllerAdviceBean > beans = new ArrayList <ControllerAdviceBean >();
84
92
for (String name : applicationContext .getBeanDefinitionNames ()) {
85
93
if (applicationContext .findAnnotationOnBean (name , ControllerAdvice .class ) != null ) {
@@ -90,12 +98,9 @@ public static List<ControllerAdviceBean> findBeans(ApplicationContext applicatio
90
98
}
91
99
92
100
/**
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.
94
103
*/
95
- public Object resolveBean () {
96
- return (this .bean instanceof String ) ? this .beanFactory .getBean ((String ) this .bean ) : this .bean ;
97
- }
98
-
99
104
public int getOrder () {
100
105
return this .order ;
101
106
}
@@ -111,6 +116,13 @@ public Class<?> getBeanType() {
111
116
return ClassUtils .getUserClass (clazz );
112
117
}
113
118
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
+
114
126
@ Override
115
127
public boolean equals (Object o ) {
116
128
if (this == o ) {
0 commit comments