Package org.springframework.web.method

# Class ControllerAdviceBean

java.lang.Object
org.springframework.web.method.ControllerAdviceBean

All Implemented Interfaces:
  `org.springframework.core.Ordered`

public class ControllerAdviceBean extends Object implements org.springframework.core.Ordered
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.

This class is internal to Spring Framework and is not meant to be used by applications to manually create `@ControllerAdvice` beans.

Since:
  3.2
Author:
  Rossen Stoyanchev, Brian Clozel, Juergen Hoeller, Sam Brannen

## Field Summary

### Fields inherited from interface org.springframework.core.Ordered

`HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE`

## Constructor Summary

Constructors
Constructor
Description
`ControllerAdviceBean(String beanName, org.springframework.beans.factory.BeanFactory beanFactory, ControllerAdvice controllerAdvice)`
Create a `ControllerAdviceBean` using the given bean name, `BeanFactory`, and `@ControllerAdvice` annotation.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`equals(Object other)`
`static List<ControllerAdviceBean>`
`findAnnotatedBeans(org.springframework.context.ApplicationContext context)`
Find beans annotated with `@ControllerAdvice` in the given `ApplicationContext` and wrap them as `ControllerAdviceBean` instances.
`Class<?>`
`getBeanType()`
Return the type of the contained bean.
`int`
`getOrder()`
Get the order value for the contained bean.
`int`
`hashCode()`
`boolean`
`isApplicableToBeanType(Class<?> beanType)`
Check whether the given bean type should be advised by this `ControllerAdviceBean`.
`Object`
`resolveBean()`
Get the bean instance for this `ControllerAdviceBean`, if necessary resolving the bean name through the `BeanFactory`.
`String`
`toString()`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## Constructor Details

### ControllerAdviceBean

public ControllerAdviceBean(String beanName, org.springframework.beans.factory.BeanFactory beanFactory, ControllerAdvice controllerAdvice)
Create a `ControllerAdviceBean` using the given bean name, `BeanFactory`, and `@ControllerAdvice` annotation.

Parameters:
  `beanName` - the name of the bean
  `beanFactory` - a `BeanFactory` to retrieve the bean type initially and later to resolve the actual bean
  `controllerAdvice` - the `@ControllerAdvice` annotation for the bean
Since:
  5.2

## Method Details

### getOrder

public int getOrder()
Get the order value for the contained bean.

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.

- If the resolved bean implements `Ordered`, use the value returned by `Ordered.getOrder()`.
- If the factory method is known, use the value returned by `OrderUtils.getOrder(AnnotatedElement)`.
- If the bean type is known, use the value returned by `OrderUtils.getOrder(Class, int)` with `Ordered.LOWEST_PRECEDENCE` used as the default order value.
- Otherwise use `Ordered.LOWEST_PRECEDENCE` as the default, fallback order value.

Specified by:
  `getOrder` in interface `org.springframework.core.Ordered`
See Also:
  - `resolveBean()`

### getBeanType

@Nullable 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.

### resolveBean

public Object resolveBean()
Get the bean instance for this `ControllerAdviceBean`, if necessary resolving the bean name through the `BeanFactory`.

Once the bean instance has been resolved it will be cached if it is a singleton, thereby avoiding repeated lookups in the `BeanFactory`.

### isApplicableToBeanType

public boolean isApplicableToBeanType(@Nullable Class<?> beanType)
Check whether the given bean type should be advised by this `ControllerAdviceBean`.

Parameters:
  `beanType` - the type of the bean to check
Since:
  4.0
See Also:
  - `ControllerAdvice`

### equals

public boolean equals(@Nullable Object other)

Overrides:
  `equals` in class `Object`

### hashCode

public int hashCode()

Overrides:
  `hashCode` in class `Object`

### toString

public String toString()

Overrides:
  `toString` in class `Object`

### findAnnotatedBeans

public static List<ControllerAdviceBean> findAnnotatedBeans(org.springframework.context.ApplicationContext context)
Find beans annotated with `@ControllerAdvice` in the given `ApplicationContext` and wrap them as `ControllerAdviceBean` instances.

Note that the `ControllerAdviceBean` instances in the returned list are sorted using `OrderComparator.sort(List)`.

See Also:
  - `getOrder()`
  - `OrderComparator`
  - `Ordered`