java.lang.Object
   ↳ org.springframework.aop.framework.ProxyConfig
     ↳ org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

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 ProxyFactoryBean, 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. The default concrete implementation is BeanNameAutoProxyCreator, identifying the beans to be proxied via a list of bean 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 TargetSource. If there are no TargetSourceCreators set, or if none matches, a SingletonTargetSource will be used by default to wrap the target bean instance.

Summary

[Expand]
Inherited Constants
From interface org.springframework.core.Ordered
Fields
protected static final Object[] DO_NOT_PROXY Convenience constant for subclasses: Return value for "do not proxy".
protected static final Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".
protected final Log logger Logger available to subclasses
Public Constructors
AbstractAutoProxyCreator()
Public Methods
Constructor[]<?> determineCandidateConstructors(Class<?> beanClass, String beanName)
Determine the candidate constructors to use for the given bean.
Object getEarlyBeanReference(Object bean, String beanName)
Obtain a reference for early access to the specified bean, typically for the purpose of resolving a circular reference.
final int getOrder()
Return the order value of this object, with a higher value meaning greater in terms of sorting.
boolean isFrozen()
Return whether the config is frozen, and no advice changes can be made.
Object postProcessAfterInitialization(Object bean, String beanName)
Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.
boolean postProcessAfterInstantiation(Object bean, String beanName)
Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.
Object postProcessBeforeInitialization(Object bean, String beanName)
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).
Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName)
Apply this BeanPostProcessor before the target bean gets instantiated.
PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
Post-process the given property values before the factory applies them to the given bean.
Class<?> predictBeanType(Class<?> beanClass, String beanName)
Predict the type of the bean to be eventually returned from this processor's postProcessBeforeInstantiation(Class, String) callback.
void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)
Specify the AdvisorAdapterRegistry to use.
void setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst)
Set whether the common interceptors should be applied before bean-specific ones.
void setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean class loader to a bean instance.
void setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
void setCustomTargetSourceCreators(TargetSourceCreator[] targetSourceCreators)
Set custom TargetSourceCreators to be applied in this order.
void setFrozen(boolean frozen)
Set whether or not the proxy should be frozen, preventing advice from being added to it once it is created.
void setInterceptorNames(String[] interceptorNames)
Set the common interceptors.
final void setOrder(int order)
Set the ordering which will apply to this class's implementation of Ordered, used when applying multiple BeanPostProcessors.
void setProxyClassLoader(ClassLoader classLoader)
Set the ClassLoader to generate the proxy class in.
Protected Methods
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.
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.
Object createProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource)
Create an AOP proxy for the given bean.
void customizeProxyFactory(ProxyFactory proxyFactory)
Subclasses may choose to implement this: for example, to change the interfaces exposed.
abstract Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource)
Return whether the given bean is to be proxied, what additional advices (e.g.
BeanFactory getBeanFactory()
Return the owning BeanFactory.
Object getCacheKey(Class<?> beanClass, String beanName)
Build a cache key for the given bean class and bean name.
TargetSource getCustomTargetSource(Class<?> beanClass, String beanName)
Create a target source for bean instances.
boolean isInfrastructureClass(Class<?> beanClass)
Return whether the given bean class represents an infrastructure class that should never be proxied.
boolean shouldProxyTargetClass(Class<?> beanClass, String beanName)
Determine whether the given bean should be proxied with its target class rather than its interfaces.
boolean shouldSkip(Class<?> beanClass, String beanName)
Subclasses should override this method to return true if the given bean should not be considered for auto-proxying by this post-processor.
Object wrapIfNecessary(Object bean, String beanName, Object cacheKey)
Wrap the given bean if necessary, i.e.
[Expand]
Inherited Methods
From class org.springframework.aop.framework.ProxyConfig
From class java.lang.Object
From interface org.springframework.beans.factory.BeanClassLoaderAware
From interface org.springframework.beans.factory.BeanFactoryAware
From interface org.springframework.beans.factory.config.BeanPostProcessor
From interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
From interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
From interface org.springframework.core.Ordered

Fields

protected static final Object[] DO_NOT_PROXY

Convenience constant for subclasses: Return value for "do not proxy".

protected static final Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS

Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".

protected final Log logger

Logger available to subclasses

Public Constructors

public AbstractAutoProxyCreator ()

Public Methods

public Constructor[]<?> determineCandidateConstructors (Class<?> beanClass, String beanName)

Determine the candidate constructors to use for the given bean.

Parameters
beanClass the raw class of the bean (never null)
beanName the name of the bean
Returns
  • the candidate constructors, or null if none specified

public Object getEarlyBeanReference (Object bean, String beanName)

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 postProcessBeforeInitialization(Object, String) / postProcessAfterInitialization(Object, String) would expose otherwise. Note that the object returned by this method will be used as bean reference unless the post-processor returns a different wrapper from said post-process callbacks. In other words: Those post-process callbacks may either eventually expose the same reference or alternatively return the raw bean instance from those subsequent callbacks (if the wrapper for the affected bean has been built for a call to this method already, it will be exposes as final bean reference by default).

Parameters
bean the raw bean instance
beanName the name of the bean
Returns
  • the object to expose as bean reference (typically with the passed-in bean instance as default)

public final int getOrder ()

Return the order value of this object, with a higher value meaning greater in terms of sorting.

Normally starting with 0, with Integer.MAX_VALUE indicating the greatest value. Same order values will result in arbitrary positions for the affected objects.

Higher values can be interpreted as lower priority. As a consequence, the object with the lowest value has highest priority (somewhat analogous to Servlet "load-on-startup" values).

Returns
  • the order value

public boolean isFrozen ()

Return whether the config is frozen, and no advice changes can be made.

public Object postProcessAfterInitialization (Object bean, String beanName)

Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.

Parameters
bean the new bean instance
beanName the name of the bean
Returns
  • the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked

public boolean postProcessAfterInstantiation (Object bean, String beanName)

Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.

This is the ideal callback for performing field injection on the given bean instance. See Spring's own AutowiredAnnotationBeanPostProcessor for a typical example.

Parameters
bean the bean instance created, with properties not having been set yet
beanName the name of the bean
Returns
  • true if properties should be set on the bean; false if property population should be skipped. Normal implementations should return true. Returning false will also prevent any subsequent InstantiationAwareBeanPostProcessor instances being invoked on this bean instance.

public Object postProcessBeforeInitialization (Object bean, String beanName)

Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.

Parameters
bean the new bean instance
beanName the name of the bean
Returns
  • the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked

public Object postProcessBeforeInstantiation (Class<?> beanClass, String beanName)

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 postProcessAfterInitialization(Object, String) callback from the configured BeanPostProcessors.

This callback will only be applied to bean definitions with a bean class. In particular, it will not be applied to beans with a "factory-method".

Post-processors may implement the extended SmartInstantiationAwareBeanPostProcessor interface in order to predict the type of the bean object that they are going to return here.

Parameters
beanClass the class of the bean to be instantiated
beanName the name of the bean
Returns
  • the bean object to expose instead of a default instance of the target bean, or null to proceed with default instantiation

public PropertyValues postProcessPropertyValues (PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)

Post-process the given property values before the factory applies them to the given bean. Allows for checking whether all dependencies have been satisfied, for example based on a "Required" annotation on bean property setters.

Also allows for replacing the property values to apply, typically through creating a new MutablePropertyValues instance based on the original PropertyValues, adding or removing specific values.

Parameters
pvs the property values that the factory is about to apply (never null)
pds the relevant property descriptors for the target bean (with ignored dependency types - which the factory handles specifically - already filtered out)
bean the bean instance created, but whose properties have not yet been set
beanName the name of the bean
Returns
  • the actual property values to apply to to the given bean (can be the passed-in PropertyValues instance), or null to skip property population

public Class<?> predictBeanType (Class<?> beanClass, String beanName)

Predict the type of the bean to be eventually returned from this processor's postProcessBeforeInstantiation(Class, String) callback.

Parameters
beanClass the raw class of the bean
beanName the name of the bean
Returns
  • the type of the bean, or null if not predictable

public void setAdvisorAdapterRegistry (AdvisorAdapterRegistry advisorAdapterRegistry)

Specify the AdvisorAdapterRegistry to use. Default is the global AdvisorAdapterRegistry.

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.

public void setBeanClassLoader (ClassLoader classLoader)

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's afterPropertiesSet() method or a custom init-method.

Parameters
classLoader the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via getDefaultClassLoader()

public void setBeanFactory (BeanFactory beanFactory)

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 afterPropertiesSet() or a custom init-method.

Parameters
beanFactory owning BeanFactory (never null). The bean can immediately call methods on the factory.

public void setCustomTargetSourceCreators (TargetSourceCreator[] targetSourceCreators)

Set custom TargetSourceCreators to be applied in this order. If the list is empty, or they all return null, a SingletonTargetSource will be created for each bean.

Note that TargetSourceCreators will kick in even for target beans where no advices or advisors have been found. If a TargetSourceCreator returns a TargetSource for a specific bean, that bean will be proxied in any case.

TargetSourceCreators can only be invoked if this post processor is used in a BeanFactory, and its BeanFactoryAware callback is used.

Parameters
targetSourceCreators list of TargetSourceCreator. Ordering is significant: The TargetSource returned from the first matching TargetSourceCreator (that is, the first that returns non-null) will be used.

public void setFrozen (boolean frozen)

Set whether or not the proxy should be frozen, preventing advice from being added to it once it is created.

Overridden from the super class to prevent the proxy configuration from being frozen before the proxy is created.

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.

public final void setOrder (int order)

Set the ordering which will apply to this class's implementation of Ordered, used when applying multiple BeanPostProcessors.

Default value is Integer.MAX_VALUE, meaning that it's non-ordered.

Parameters
order ordering value

public void setProxyClassLoader (ClassLoader classLoader)

Set the ClassLoader to generate the proxy class in.

Default is the bean ClassLoader, i.e. the ClassLoader used by the containing BeanFactory for loading all bean classes. This can be overridden here for specific proxies.

Protected Methods

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 false. Subclasses may override this if they will always return pre-filtered Advisors.

Returns
  • whether the Advisors are pre-filtered

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.

Parameters
beanName the name of the bean
specificInterceptors the set of interceptors that is specific to this bean (may be empty, but not null)
Returns
  • the list of Advisors for the given bean

protected Object createProxy (Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource)

Create an AOP proxy for the given bean.

Parameters
beanClass the class of the bean
beanName the name of the bean
specificInterceptors the set of interceptors that is specific to this bean (may be empty, but not null)
targetSource the TargetSource for the proxy, already pre-configured to access the bean
Returns
  • the AOP proxy for the bean

protected void customizeProxyFactory (ProxyFactory proxyFactory)

Subclasses may choose to implement this: for example, to change the interfaces exposed.

The default implementation is empty.

Parameters
proxyFactory ProxyFactory that is already configured with TargetSource and interfaces and will be used to create the proxy immediably after this method returns

protected abstract Object[] getAdvicesAndAdvisorsForBean (Class<?> beanClass, String beanName, TargetSource customTargetSource)

Return whether the given bean is to be proxied, what additional advices (e.g. AOP Alliance interceptors) and advisors to apply.

Parameters
beanClass the class of the bean to advise
beanName the name of the bean
customTargetSource the TargetSource returned by the getCustomTargetSource(Class, String) method: may be ignored. Will be null if no custom target source is in use.
Returns
  • an array of additional interceptors for the particular bean; or an empty array if no additional interceptors but just the common ones; or null if no proxy at all, not even with the common interceptors. See constants DO_NOT_PROXY and PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS.
Throws
BeansException in case of errors

protected BeanFactory getBeanFactory ()

Return the owning BeanFactory. May be null, as this object doesn't need to belong to a bean factory.

protected Object getCacheKey (Class<?> beanClass, String beanName)

Build a cache key for the given bean class and bean name.

Parameters
beanClass the bean class
beanName the bean name
Returns
  • the cache key for the given class and name

protected TargetSource getCustomTargetSource (Class<?> beanClass, String beanName)

Create a target source for bean instances. Uses any TargetSourceCreators if set. Returns null if no custom TargetSource should be used.

This implementation uses the "customTargetSourceCreators" property. Subclasses can override this method to use a different mechanism.

Parameters
beanClass the class of the bean to create a TargetSource for
beanName the name of the bean
Returns
  • a TargetSource for this bean

protected boolean isInfrastructureClass (Class<?> beanClass)

Return whether the given bean class represents an infrastructure class that should never be proxied.

Default implementation considers Advisors, Advices and AbstractAutoProxyCreators as infrastructure classes.

Parameters
beanClass the class of the bean
Returns
  • whether the bean represents an infrastructure class
See Also

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 "proxyTargetClass" setting as well as the "preserveTargetClass" attribute of the corresponding bean definition.

Parameters
beanClass the class of the bean
beanName the name of the bean
Returns
  • whether the given bean should be proxied with its target class

protected boolean shouldSkip (Class<?> beanClass, String beanName)

Subclasses should override this method to return true if the given bean should not be considered for auto-proxying by this post-processor.

Sometimes we need to be able to avoid this happening if it will lead to a circular reference. This implementation returns false.

Parameters
beanClass the class of the bean
beanName the name of the bean
Returns
  • whether to skip the given bean

protected Object wrapIfNecessary (Object bean, String beanName, Object cacheKey)

Wrap the given bean if necessary, i.e. if it is eligible for being proxied.

Parameters
bean the raw bean instance
beanName the name of the bean
cacheKey the cache key for metadata access
Returns
  • a proxy wrapping the bean, or the raw bean instance as-is