public abstract class AbstractAutoProxyCreator
extends org.springframework.aop.framework.ProxyConfig
implements org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.core.Ordered, org.springframework.aop.framework.AopInfrastructureBean
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.
setInterceptorNames(java.lang.String[])
,
getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource)
,
BeanNameAutoProxyCreator
,
DefaultAdvisorAutoProxyCreator
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected static Object[] |
DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy".
|
protected org.apache.commons.logging.Log |
logger
Logger available to subclasses
|
protected static Object[] |
PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for
"proxy without additional interceptors, just the common ones".
|
Constructor and Description |
---|
AbstractAutoProxyCreator() |
Modifier and Type | Method and Description |
---|---|
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.
|
protected org.springframework.aop.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.
|
protected Object |
createProxy(Class beanClass,
String beanName,
Object[] specificInterceptors,
org.springframework.aop.TargetSource targetSource)
Create an AOP proxy for the given bean.
|
protected void |
customizeProxyFactory(org.springframework.aop.framework.ProxyFactory proxyFactory)
Subclasses may choose to implement this: for example, to change the interfaces exposed.
|
Constructor[] |
determineCandidateConstructors(Class beanClass,
String beanName) |
protected abstract Object[] |
getAdvicesAndAdvisorsForBean(Class beanClass,
String beanName,
org.springframework.aop.TargetSource customTargetSource)
Return whether the given bean is to be proxied, what additional advices (e.g.
|
protected org.springframework.beans.factory.BeanFactory |
getBeanFactory()
Return the owning BeanFactory.
|
protected Object |
getCacheKey(Class beanClass,
String beanName)
Build a cache key for the given bean class and bean name.
|
protected org.springframework.aop.TargetSource |
getCustomTargetSource(Class beanClass,
String beanName)
Create a target source for bean instances.
|
Object |
getEarlyBeanReference(Object bean,
String beanName) |
int |
getOrder() |
boolean |
isFrozen() |
protected boolean |
isInfrastructureClass(Class beanClass)
Return whether the given bean class represents an infrastructure class that should never be proxied.
|
protected boolean |
isInfrastructureClass(Class beanClass,
String beanName)
Deprecated.
since 4.2 Use
isInfrastructureClass(Class) |
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) |
Object |
postProcessBeforeInitialization(Object bean,
String beanName) |
Object |
postProcessBeforeInstantiation(Class beanClass,
String beanName) |
org.springframework.beans.PropertyValues |
postProcessPropertyValues(org.springframework.beans.PropertyValues pvs,
PropertyDescriptor[] pds,
Object bean,
String beanName) |
Class |
predictBeanType(Class beanClass,
String beanName) |
void |
setAdvisorAdapterRegistry(org.springframework.aop.framework.adapter.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) |
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) |
void |
setCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.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.
|
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 boolean |
shouldProxyTargetClass(Class beanClass,
String beanName)
Determine whether the given bean should be proxied with its target class rather than its interfaces.
|
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. |
protected Object |
wrapIfNecessary(Object bean,
String beanName,
Object cacheKey)
Wrap the given bean if necessary, i.e.
|
protected static final Object[] DO_NOT_PROXY
protected static final Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
protected final org.apache.commons.logging.Log logger
public final void setOrder(int order)
Default value is Integer.MAX_VALUE
, meaning that it's non-ordered.
order
- ordering valuepublic final int getOrder()
getOrder
in interface org.springframework.core.Ordered
public void setFrozen(boolean frozen)
Overridden from the super class to prevent the proxy configuration from being frozen before the proxy is created.
setFrozen
in class org.springframework.aop.framework.ProxyConfig
public boolean isFrozen()
isFrozen
in class org.springframework.aop.framework.ProxyConfig
public void setAdvisorAdapterRegistry(org.springframework.aop.framework.adapter.AdvisorAdapterRegistry advisorAdapterRegistry)
GlobalAdvisorAdapterRegistry
public void setCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.TargetSourceCreator[] targetSourceCreators)
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.
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 setInterceptorNames(String[] interceptorNames)
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 void setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst)
public void setProxyClassLoader(ClassLoader classLoader)
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.
public void setBeanClassLoader(ClassLoader classLoader)
setBeanClassLoader
in interface org.springframework.beans.factory.BeanClassLoaderAware
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
setBeanFactory
in interface org.springframework.beans.factory.BeanFactoryAware
protected org.springframework.beans.factory.BeanFactory getBeanFactory()
null
, as this object doesn't need to belong to a bean factory.public Class predictBeanType(Class beanClass, String beanName)
predictBeanType
in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws org.springframework.beans.BeansException
determineCandidateConstructors
in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
org.springframework.beans.BeansException
public Object getEarlyBeanReference(Object bean, String beanName) throws org.springframework.beans.BeansException
getEarlyBeanReference
in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
org.springframework.beans.BeansException
public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws org.springframework.beans.BeansException
postProcessBeforeInstantiation
in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
org.springframework.beans.BeansException
public boolean postProcessAfterInstantiation(Object bean, String beanName)
postProcessAfterInstantiation
in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
public org.springframework.beans.PropertyValues postProcessPropertyValues(org.springframework.beans.PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
postProcessPropertyValues
in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
public Object postProcessBeforeInitialization(Object bean, String beanName)
postProcessBeforeInitialization
in interface org.springframework.beans.factory.config.BeanPostProcessor
public Object postProcessAfterInitialization(Object bean, String beanName) throws org.springframework.beans.BeansException
postProcessAfterInitialization
in interface org.springframework.beans.factory.config.BeanPostProcessor
org.springframework.beans.BeansException
getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource)
protected Object getCacheKey(Class beanClass, String beanName)
beanClass
- the bean classbeanName
- the bean nameprotected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey)
bean
- the raw bean instancebeanName
- the name of the beancacheKey
- the cache key for metadata access@Deprecated protected boolean isInfrastructureClass(Class beanClass, String beanName)
isInfrastructureClass(Class)
protected boolean isInfrastructureClass(Class beanClass)
Default implementation considers Advisors, Advices and AbstractAutoProxyCreators as infrastructure classes.
beanClass
- the class of the beanAdvisor
,
MethodInterceptor
,
shouldSkip(java.lang.Class, java.lang.String)
protected boolean shouldSkip(Class beanClass, String beanName)
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
.
beanClass
- the class of the beanbeanName
- the name of the beanprotected org.springframework.aop.TargetSource getCustomTargetSource(Class beanClass, String beanName)
null
if no
custom TargetSource should be used.
This implementation uses the "customTargetSourceCreators" property. Subclasses can override this method to use a different mechanism.
beanClass
- the class of the bean to create a TargetSource forbeanName
- the name of the beansetCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.TargetSourceCreator[])
protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors, org.springframework.aop.TargetSource targetSource)
beanClass
- the class of the beanbeanName
- the name of the beanspecificInterceptors
- 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 beanbuildAdvisors(java.lang.String, java.lang.Object[])
protected boolean shouldProxyTargetClass(Class beanClass, String beanName)
"proxyTargetClass" setting
as well as the
"preserveTargetClass" attribute
of the corresponding bean
definition.beanClass
- the class of the beanbeanName
- the name of the beanAutoProxyUtils.shouldProxyTargetClass(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.lang.String)
protected boolean advisorsPreFiltered()
Default is false
. Subclasses may override this if they will always return pre-filtered Advisors.
getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource)
,
Advised.setPreFiltered(boolean)
protected org.springframework.aop.Advisor[] buildAdvisors(String beanName, Object[] specificInterceptors)
beanName
- the name of the beanspecificInterceptors
- the set of interceptors that is specific to this bean (may be empty, but not null)protected void customizeProxyFactory(org.springframework.aop.framework.ProxyFactory proxyFactory)
The default implementation is empty.
proxyFactory
- ProxyFactory that is already configured with TargetSource and interfaces and will be used to create
the proxy immediably after this method returnsprotected abstract Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, org.springframework.aop.TargetSource customTargetSource) throws org.springframework.beans.BeansException
beanClass
- the class of the bean to advisebeanName
- the name of the beancustomTargetSource
- the TargetSource returned by the getCustomTargetSource(java.lang.Class, java.lang.String)
method: may be ignored. Will be
null
if no custom target source is in use.null
if no proxy at all, not even with the common
interceptors. See constants DO_NOT_PROXY and PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS.org.springframework.beans.BeansException
- in case of errorsDO_NOT_PROXY
,
PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Copyright © 2003–2016 Atlassian. All rights reserved.