org.springframework.beans.factory.config.BeanPostProcessor implementation
that wraps each eligible bean with an AOP proxy, delegating to specified interceptors
before invoking the bean itself.
This class distinguishes between "common" interceptors: shared for all proxies it
creates, and "specific" interceptors: unique per bean instance. There need not be any
common interceptors. If there are, they are set using the interceptorNames property.
As with org., interceptors names
in the current factory are used rather than bean references to allow correct handling
of prototype advisors and interceptors: for example, to support stateful mixins.
Any advice type is supported for "interceptorNames" entries.
Such auto-proxying is particularly useful if there's a large number of beans that need to be wrapped with similar proxies, i.e. delegating to the same interceptors. Instead of x repetitive proxy definitions for x target beans, you can register one single such post processor with the bean factory to achieve the same effect.
Subclasses can apply any strategy to decide if a bean is to be proxied, e.g. by type,
by name, by definition details, etc. They can also return additional interceptors that
should just be applied to the specific bean instance. A simple concrete implementation is
BeanNameAutoProxyCreator, identifying the beans to be proxied via given names.
Any number of TargetSourceCreator implementations can be used to create
a custom target source: for example, to pool prototype objects. Auto-proxying will
occur even if there is no advice, as long as a TargetSourceCreator specifies a custom
org.. If there are no TargetSourceCreators set,
or if none matches, a org.
will be used by default to wrap the target bean instance.
setInterceptorNames, getAdvicesAndAdvisorsForBean, BeanNameAutoProxyCreator, DefaultAdvisorAutoProxyCreator
| Modifier and Type | Field and Description |
|---|---|
| private final Map | |
| private AdvisorAdapterRegistry | advisorAdapterRegistry
Default is global AdvisorAdapterRegistry. |
| private boolean | |
| private BeanFactory | |
| private TargetSourceCreator[] | |
| protected static final Object[] | DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy". |
| private final Map | |
| private boolean | freezeProxy
Indicates whether the proxy should be frozen. |
| private String[] | interceptorNames
Default is no common interceptors. |
| protected final Log | logger
Logger available to subclasses. |
| protected static final Object[] | PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones". |
| private final Map | |
| private final Set |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected boolean | Returns: whether the Advisors are pre-filteredReturn whether the Advisors returned by the subclass are pre-filtered to match the bean's target class already, allowing the ClassFilter check to be skipped when building advisors chains for AOP invocations. |
| protected Advisor[] | Returns: the list of Advisors for the given beanthe name of the bean beanName, Object[] the set of interceptors that is
specific to this bean (may be empty, but not null) specificInterceptors)Determine the advisors for the given bean, including the specific interceptors as well as the common interceptor, all adapted to the Advisor interface. |
| private Object | buildProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource, boolean classOnly)
|
| protected Object | Returns: the AOP proxy for the beanthe class of the bean beanClass, String the name of the bean beanName, Object[] the set of interceptors that is
specific to this bean (may be empty, but not null) specificInterceptors, TargetSource the TargetSource for the proxy,
already pre-configured to access the bean targetSource)Create an AOP proxy for the given bean. |
| private Class | createProxyClass(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource)
|
| protected void | customizeProxyFactory(ProxyFactory
a ProxyFactory that is already configured with
TargetSource and interfaces and will be used to create the proxy
immediately after this method returns proxyFactory)Subclasses may choose to implement this: for example, to change the interfaces exposed. |
| public Class | determineBeanType(Class<?>
the raw class of the bean beanClass, String the name of the bean beanName)Overrides default org. Determine the type of the bean to be eventually returned from this
processor's |
| public Constructor | determineCandidateConstructors(Class<?>
the raw class of the bean (never beanClass, String null)the name of the bean beanName)Overrides default org. Determine the candidate constructors to use for the given bean. |
| protected abstract Object[] | Returns: an array of additional interceptors for the particular bean; or an empty array if no additional interceptors but just the common ones; ornull if no proxy at all, not even with the common interceptors.
See constants DO_NOT_PROXY and PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS.the class of the bean to advise beanClass, String the name of the bean beanName, TargetSource the TargetSource returned by the
customTargetSource)getCustomTargetSource method: may be ignored.
Will be null if no custom target source is in use.Return whether the given bean is to be proxied, what additional advices (e.g. AOP Alliance interceptors) and advisors to apply. |
| protected BeanFactory | |
| protected Object | Returns: the cache key for the given class and namethe bean class beanClass, String the bean name beanName)Build a cache key for the given bean class and bean name. |
| protected TargetSource | Returns: a TargetSource for this beanthe class of the bean to create a TargetSource for beanClass, String the name of the bean beanName)Create a target source for bean instances. |
| public Object | getEarlyBeanReference(Object
the raw bean instance bean, String the name of the bean beanName)Overrides default org. Obtain a reference for early access to the specified bean, typically for the purpose of resolving a circular reference. |
| public boolean | isFrozen()
Overrides org. Return whether the config is frozen, and no advice changes can be made. |
| protected boolean | Returns: whether the bean represents an infrastructure classthe class of the bean beanClass)Return whether the given bean class represents an infrastructure class that should never be proxied. |
| public Object | postProcessAfterInitialization(Object
the new bean instance bean, String the name of the bean beanName)Overrides default org. Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass. |
| public Object | postProcessBeforeInstantiation(Class<?>
the class of the bean to be instantiated beanClass, String the name of the bean beanName)Overrides default org. Apply this BeanPostProcessor before the target bean gets instantiated. |
| public PropertyValues | postProcessProperties(PropertyValues
the property values that the factory is about to apply (never pvs, Object null)the bean instance created, but whose properties have not yet been set bean, String the name of the bean beanName)Overrides default org. Post-process the given property values before the factory applies them to the given bean. |
| public Class | predictBeanType(Class<?>
the raw class of the bean beanClass, String the name of the bean beanName)Overrides default org. Predict the type of the bean to be eventually returned from this
processor's |
| private Advisor[] | |
| public void | setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)
Specify the |
| public void | setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst)
Set whether the common interceptors should be applied before bean-specific ones. |
| public void | setBeanFactory(BeanFactory
owning BeanFactory (never beanFactory)null).
The bean can immediately call methods on the factory.Implements org. Callback that supplies the owning factory to a bean instance. |
| public void | setCustomTargetSourceCreators(TargetSourceCreator...
the list of targetSourceCreators)TargetSourceCreators.
Ordering is significant: The TargetSource returned from the first matching
TargetSourceCreator (that is, the first that returns non-null) will be used.Set custom |
| public void | setFrozen(boolean frozen)
Overrides org. Set whether the proxy should be frozen, preventing advice from being added to it once it is created. |
| public void | |
| protected boolean | Returns: whether the given bean should be proxied with its target classthe class of the bean beanClass, String the name of the bean beanName)Determine whether the given bean should be proxied with its target class rather than its interfaces. |
| protected boolean | Returns: whether to skip the given beanthe class of the bean beanClass, String the name of the bean beanName)Subclasses should override this method to return |
| protected Object | Returns: a proxy wrapping the bean, or the raw bean instance as-isthe raw bean instance bean, String the name of the bean beanName, Object the cache key for metadata access cacheKey)Wrap the given bean if necessary, i.e. if it is eligible for being proxied. |
| advisedBeans | back to summary |
|---|---|
| private final Map<Object, Boolean> advisedBeans | |
| advisorAdapterRegistry | back to summary |
|---|---|
| private AdvisorAdapterRegistry advisorAdapterRegistry Default is global AdvisorAdapterRegistry. | |
| applyCommonInterceptorsFirst | back to summary |
|---|---|
| private boolean applyCommonInterceptorsFirst | |
| beanFactory | back to summary |
|---|---|
| private BeanFactory beanFactory
| |
| customTargetSourceCreators | back to summary |
|---|---|
| private TargetSourceCreator[] customTargetSourceCreators
| |
| DO_NOT_PROXY | back to summary |
|---|---|
| protected static final Object[] DO_NOT_PROXY Convenience constant for subclasses: Return value for "do not proxy".
| |
| earlyBeanReferences | back to summary |
|---|---|
| private final Map<Object, Object> earlyBeanReferences | |
| freezeProxy | back to summary |
|---|---|
| private boolean freezeProxy Indicates whether the proxy should be frozen. Overridden from super to prevent the configuration from becoming frozen too early. | |
| interceptorNames | back to summary |
|---|---|
| private String[] interceptorNames Default is no common interceptors. | |
| logger | back to summary |
|---|---|
| protected final Log logger Logger available to subclasses. | |
| PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS | back to summary |
|---|---|
| protected static final Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".
| |
| proxyTypes | back to summary |
|---|---|
| private final Map<Object, Class<?>> proxyTypes | |
| targetSourcedBeans | back to summary |
|---|---|
| private final Set<String> targetSourcedBeans | |
| AbstractAutoProxyCreator | back to summary |
|---|---|
| public AbstractAutoProxyCreator() | |
| advisorsPreFiltered | back to summary |
|---|---|
| protected boolean advisorsPreFiltered() Return whether the Advisors returned by the subclass are pre-filtered to match the bean's target class already, allowing the ClassFilter check to be skipped when building advisors chains for AOP invocations. Default is
| |
| buildAdvisors | back to summary |
|---|---|
| protected Advisor[] buildAdvisors(String beanName, Object[] specificInterceptors) Determine the advisors for the given bean, including the specific interceptors as well as the common interceptor, all adapted to the Advisor interface. | |
| buildProxy | back to summary |
|---|---|
| private Object buildProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource, boolean classOnly) | |
| createProxy | back to summary |
|---|---|
| protected Object createProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) Create an AOP proxy for the given bean.
| |
| createProxyClass | back to summary |
|---|---|
| private Class | |
| customizeProxyFactory | back to summary |
|---|---|
| protected void customizeProxyFactory(ProxyFactory proxyFactory) Subclasses may choose to implement this: for example, to change the interfaces exposed. The default implementation is empty.
| |
| determineBeanType | back to summary |
|---|---|
| public Class Overrides default org. Doc from org. Determine the type of the bean to be eventually returned from this
processor's The default implementation returns the given bean class as-is. Specific implementations should fully evaluate their processing steps in order to create/initialize a potential proxy class upfront. | |
| determineCandidateConstructors | back to summary |
|---|---|
| public Constructor Overrides default org. Determine the candidate constructors to use for the given bean. The default implementation returns
| |
| getAdvicesAndAdvisorsForBean | back to summary |
|---|---|
| protected abstract Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource) throws BeansException Return whether the given bean is to be proxied, what additional advices (e.g. AOP Alliance interceptors) and advisors to apply.
| |
| getBeanFactory | back to summary |
|---|---|
| protected BeanFactory getBeanFactory() Return the owning
| |
| getCacheKey | back to summary |
|---|---|
| protected Object getCacheKey(Class<?> beanClass, String beanName) Build a cache key for the given bean class and bean name. Note As of 4.2.3, this implementation does not return a concatenated
class/name String anymore but rather the most efficient cache key possible:
a plain bean name, prepended with | |
| getCustomTargetSource | back to summary |
|---|---|
| protected TargetSource getCustomTargetSource(Class<?> beanClass, String beanName) Create a target source for bean instances. Uses any TargetSourceCreators if set.
Returns This implementation uses the "customTargetSourceCreators" property. Subclasses can override this method to use a different mechanism.
| |
| getEarlyBeanReference | back to summary |
|---|---|
| public Object getEarlyBeanReference(Object bean, String beanName) Overrides default org. Obtain a reference for early access to the specified bean, typically for the purpose of resolving a circular reference. This callback gives post-processors a chance to expose a wrapper
early - that is, before the target bean instance is fully initialized.
The exposed object should be equivalent to the what
The default implementation returns the given | |
| isFrozen | back to summary |
|---|---|
| public boolean isFrozen() Overrides org. Doc from org. Return whether the config is frozen, and no advice changes can be made.
| |
| isInfrastructureClass | back to summary |
|---|---|
| protected boolean isInfrastructureClass(Class<?> beanClass) Return whether the given bean class represents an infrastructure class that should never be proxied. The default implementation considers Advices, Advisors and AopInfrastructureBeans as infrastructure classes.
| |
| postProcessAfterInitialization | back to summary |
|---|---|
| public Object postProcessAfterInitialization(Object bean, String beanName) Overrides default org. Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.
| |
| postProcessBeforeInstantiation | back to summary |
|---|---|
| public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) Overrides default org. Apply this BeanPostProcessor before the target bean gets instantiated. The returned bean object may be a proxy to use instead of the target bean, effectively suppressing default instantiation of the target bean. If a non-null object is returned by this method, the bean creation process
will be short-circuited. The only further processing applied is the
This callback will be applied to bean definitions with their bean class, as well as to factory-method definitions in which case the returned bean type will be passed in here. Post-processors may implement the extended
The default implementation returns | |
| postProcessProperties | back to summary |
|---|---|
| public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) Overrides default org. Doc from org. Post-process the given property values before the factory applies them to the given bean. The default implementation returns the given
| |
| predictBeanType | back to summary |
|---|---|
| public Class Overrides default org. Doc from org. Predict the type of the bean to be eventually returned from this
processor's The default implementation returns | |
| resolveInterceptorNames | back to summary |
|---|---|
| private Advisor[] resolveInterceptorNames() Resolves the specified interceptor names to Advisor objects.
| |
| setAdvisorAdapterRegistry | back to summary |
|---|---|
| public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) Specify the Default is the global | |
| setApplyCommonInterceptorsFirst | back to summary |
|---|---|
| public void setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst) Set whether the common interceptors should be applied before bean-specific ones. Default is "true"; else, bean-specific interceptors will get applied first. | |
| setBeanFactory | back to summary |
|---|---|
| public void setBeanFactory(BeanFactory beanFactory) Implements org. Doc from org. Callback that supplies the owning factory to a bean instance. Invoked after the population of normal bean properties
but before an initialization callback such as
| |
| setCustomTargetSourceCreators | back to summary |
|---|---|
| public void setCustomTargetSourceCreators(TargetSourceCreator... targetSourceCreators) Set custom Note that TargetSourceCreators will kick in even for target beans
where no advices or advisors have been found. If a
| |
| setFrozen | back to summary |
|---|---|
| public void setFrozen(boolean frozen) Overrides org. Set whether the proxy should be frozen, preventing advice from being added to it once it is created. Overridden from the superclass to prevent the proxy configuration from being frozen before the proxy is created.
| |
| setInterceptorNames | back to summary |
|---|---|
| public void setInterceptorNames(String... interceptorNames) Set the common interceptors. These must be bean names in the current factory. They can be of any advice or advisor type Spring supports. If this property isn't set, there will be zero common interceptors. This is perfectly valid, if "specific" interceptors such as matching Advisors are all we want. | |
| shouldProxyTargetClass | back to summary |
|---|---|
| protected boolean shouldProxyTargetClass(Class<?> beanClass, String beanName) Determine whether the given bean should be proxied with its target class rather than its interfaces. Checks the
| |
| shouldSkip | back to summary |
|---|---|
| protected boolean shouldSkip(Class<?> beanClass, String beanName) Subclasses should override this method to return Sometimes we need to be able to avoid this happening, e.g. if it will lead to
a circular reference or if the existing target instance needs to be preserved.
This implementation returns
| |
| wrapIfNecessary | back to summary |
|---|---|
| protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) Wrap the given bean if necessary, i.e. if it is eligible for being proxied. | |