Top Description Fields Constructors Methods
org.springframework.web.method

public Class ControllerAdviceBean

extends Object
implements Ordered
Class Inheritance
All Implemented Interfaces
org.springframework.core.Ordered
Imports
java.lang.reflect.AnnotatedElement, .Method, java.util.ArrayList, .List, org.springframework.aop.scope.ScopedProxyUtils, org.springframework.beans.factory.BeanFactory, .BeanFactoryUtils, .ListableBeanFactory, .NoSuchBeanDefinitionException, org.springframework.beans.factory.config.BeanDefinition, .ConfigurableBeanFactory, org.springframework.beans.factory.support.RootBeanDefinition, org.springframework.context.ApplicationContext, .ConfigurableApplicationContext, org.springframework.core.OrderComparator, .Ordered, org.springframework.core.annotation.AnnotatedElementUtils, .OrderUtils, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, org.springframework.web.bind.annotation.ControllerAdvice

Encapsulates information about an @ControllerAdvice Spring-managed bean without necessarily requiring it to be instantiated.

The findAnnotatedBeans(ApplicationContext) method can be used to discover such beans. However, a ControllerAdviceBean may be created from any object, including ones without an @ControllerAdvice annotation.

Authors
Rossen Stoyanchev, Brian Clozel, Juergen Hoeller, Sam Brannen
Since
3.2

Field Summary

Modifier and TypeField and Description
private final BeanFactory
private final Object
beanOrName

Reference to the actual bean instance or a String representing the bean name.

private final Class<?>
private final HandlerTypePredicate
private final boolean
private Integer
private Object
resolvedBean

Reference to the resolved bean instance, potentially lazily retrieved via the BeanFactory.

Constructor Summary

AccessConstructor and Description
public
ControllerAdviceBean(Object
the bean instance
bean
)

Create a ControllerAdviceBean using the given bean instance.

public
ControllerAdviceBean(String
the name of the bean
beanName
,
BeanFactory
a BeanFactory to retrieve the bean type initially and later to resolve the actual bean
beanFactory
)

Create a ControllerAdviceBean using the given bean name and BeanFactory.

public
ControllerAdviceBean(String
the name of the bean
beanName
,
BeanFactory
a BeanFactory to retrieve the bean type initially and later to resolve the actual bean
beanFactory
,
ControllerAdvice
the @ControllerAdvice annotation for the bean, or null if not yet retrieved
controllerAdvice
)

Create a ControllerAdviceBean using the given bean name, BeanFactory, and @ControllerAdvice annotation.

Method Summary

Modifier and TypeMethod and Description
private static HandlerTypePredicate
private static HandlerTypePredicate
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public static List<ControllerAdviceBean>
findAnnotatedBeans(ApplicationContext context)

Find beans annotated with @ControllerAdvice in the given ApplicationContext and wrap them as ControllerAdviceBean instances.

public Class<?>
getBeanType()

Return the type of the contained bean.

private static Class<?>
getBeanType(String beanName, BeanFactory beanFactory)

public int
getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value for the contained bean.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean
isApplicableToBeanType(Class<?>
the type of the bean to check
beanType
)

Check whether the given bean type should be advised by this ControllerAdviceBean.

private BeanFactory
public Object
resolveBean()

Get the bean instance for this ControllerAdviceBean, if necessary resolving the bean name through the BeanFactory.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

beanFactoryback to summary
private final BeanFactory beanFactory
Annotations
@Nullable
beanOrNameback to summary
private final Object beanOrName

Reference to the actual bean instance or a String representing the bean name.

beanTypeback to summary
private final Class<?> beanType
Annotations
@Nullable
beanTypePredicateback to summary
private final HandlerTypePredicate beanTypePredicate
isSingletonback to summary
private final boolean isSingleton
orderback to summary
private Integer order
Annotations
@Nullable
resolvedBeanback to summary
private Object resolvedBean

Reference to the resolved bean instance, potentially lazily retrieved via the BeanFactory.

Annotations
@Nullable

Constructor Detail

ControllerAdviceBeanback to summary
public ControllerAdviceBean(Object bean)

Create a ControllerAdviceBean using the given bean instance.

Parameters
bean:Object

the bean instance

ControllerAdviceBeanback to summary
public ControllerAdviceBean(String beanName, BeanFactory beanFactory)

Create a ControllerAdviceBean using the given bean name and BeanFactory.

Parameters
beanName:String

the name of the bean

beanFactory:BeanFactory

a BeanFactory to retrieve the bean type initially and later to resolve the actual bean

ControllerAdviceBeanback to summary
public ControllerAdviceBean(String beanName, BeanFactory beanFactory, ControllerAdvice controllerAdvice)

Create a ControllerAdviceBean using the given bean name, BeanFactory, and @ControllerAdvice annotation.

Parameters
beanName:String

the name of the bean

beanFactory:BeanFactory

a BeanFactory to retrieve the bean type initially and later to resolve the actual bean

controllerAdvice:ControllerAdvice

the @ControllerAdvice annotation for the bean, or null if not yet retrieved

Since
5.2

Method Detail

createBeanTypePredicateback to summary
private static HandlerTypePredicate createBeanTypePredicate(Class<?> beanType)
createBeanTypePredicateback to summary
private static HandlerTypePredicate createBeanTypePredicate(ControllerAdvice controllerAdvice)
equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
findAnnotatedBeansback to summary
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context)

Find beans annotated with @ControllerAdvice in the given ApplicationContext and wrap them as ControllerAdviceBean instances.

As of Spring Framework 5.2, the ControllerAdviceBean instances in the returned list are sorted using OrderComparator#sort(List).

See Also
getOrder(), OrderComparator, Ordered
getBeanTypeback to summary
public Class<?> getBeanType()

Return the type of the contained bean.

If the bean type is a CGLIB-generated class, the original user-defined class is returned.

Annotations
@Nullable
getBeanTypeback to summary
private static Class<?> getBeanType(String beanName, BeanFactory beanFactory)
Annotations
@Nullable
getOrderback to summary
public int getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value for the contained bean.

As of Spring Framework 5.3, the order value is lazily retrieved using the following algorithm and cached. Note, however, that a @ControllerAdvice bean that is configured as a scoped bean — for example, as a request-scoped or session-scoped bean — will not be eagerly resolved. Consequently, Ordered is not honored for scoped @ControllerAdvice beans.

Returns:int

Doc from org.springframework.core.Ordered.getOrder.

the order value

Annotations
@Override
See Also
resolveBean()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isApplicableToBeanTypeback to summary
public boolean isApplicableToBeanType(Class<?> beanType)

Check whether the given bean type should be advised by this ControllerAdviceBean.

Parameters
beanType:Class<?>

the type of the bean to check

Since
4.0
See Also
ControllerAdvice
obtainBeanFactoryback to summary
private BeanFactory obtainBeanFactory()
resolveBeanback to summary
public Object resolveBean()

Get the bean instance for this ControllerAdviceBean, if necessary resolving the bean name through the BeanFactory.

As of Spring Framework 5.2, once the bean instance has been resolved it will be cached if it is a singleton, thereby avoiding repeated lookups in the BeanFactory.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override