类 AbstractAutoProxyCreator
- java.lang.Object
- org.springframework.aop.framework.ProxyConfig
- org.springframework.aop.framework.ProxyProcessorSupport
- org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
- 所有已实现的接口:
Serializable
,AopInfrastructureBean
,Aware
,BeanClassLoaderAware
,BeanFactoryAware
,BeanPostProcessor
,InstantiationAwareBeanPostProcessor
,SmartInstantiationAwareBeanPostProcessor
,Ordered
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
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. 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 customTargetSource
. If there are no TargetSourceCreators set, or if none matches, aSingletonTargetSource
will be used by default to wrap the target bean instance.- 从以下版本开始:
- 13.10.2003
- 作者:
- Juergen Hoeller, Rod Johnson, Rob Harrop
- 另请参阅:
setInterceptorNames(java.lang.String...)
,getAdvicesAndAdvisorsForBean(java.lang.Class<?>, java.lang.String, org.springframework.aop.TargetSource)
,BeanNameAutoProxyCreator
,DefaultAdvisorAutoProxyCreator
, 序列化表格
字段概要
字段 修饰符和类型 字段 说明 protected static Object[]
DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy".protected Log
logger
Logger available to subclassesprotected static Object[]
PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 AbstractAutoProxyCreator()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 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 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, TargetSource targetSource)
Create an AOP proxy for the given bean.protected void
customizeProxyFactory(ProxyFactory proxyFactory)
Subclasses may choose to implement this: for example, to change the interfaces exposed.Constructor<?>[]
determineCandidateConstructors(Class<?> beanClass, String beanName)
Determine the candidate constructors to use for the given bean.protected abstract Object[]
getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource)
Return whether the given bean is to be proxied, what additional advices (e.g.protected BeanFactory
getBeanFactory()
Return the owningBeanFactory
.protected Object
getCacheKey(Class<?> beanClass, String beanName)
Build a cache key for the given bean class and bean name.protected TargetSource
getCustomTargetSource(Class<?> beanClass, String beanName)
Create a target source for bean instances.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.boolean
isFrozen()
Return whether the config is frozen, and no advice changes can be made.protected boolean
isInfrastructureClass(Class<?> beanClass)
Return whether the given bean class represents an infrastructure class that should never be proxied.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'safterPropertiesSet
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'sInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
callback.void
setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)
Specify theAdvisorAdapterRegistry
to use.void
setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst)
Set whether the common interceptors should be applied before bean-specific ones.void
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.void
setCustomTargetSourceCreators(TargetSourceCreator... targetSourceCreators)
Set customTargetSourceCreators
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.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 returntrue
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. if it is eligible for being proxied.从类继承的方法 org.springframework.aop.framework.ProxyProcessorSupport
evaluateProxyInterfaces, getOrder, getProxyClassLoader, isConfigurationCallbackInterface, isInternalLanguageInterface, setBeanClassLoader, setOrder, setProxyClassLoader
从类继承的方法 org.springframework.aop.framework.ProxyConfig
copyFrom, isExposeProxy, isOpaque, isOptimize, isProxyTargetClass, setExposeProxy, setOpaque, setOptimize, setProxyTargetClass, toString
字段详细资料
DO_NOT_PROXY
protected static final Object[] DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy".
PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
protected static final Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".
构造器详细资料
AbstractAutoProxyCreator
public AbstractAutoProxyCreator()
方法详细资料
setFrozen
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.
- 覆盖:
setFrozen
在类中ProxyConfig
isFrozen
public boolean isFrozen()
从类复制的说明:ProxyConfig
Return whether the config is frozen, and no advice changes can be made.- 覆盖:
isFrozen
在类中ProxyConfig
setAdvisorAdapterRegistry
public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)
Specify theAdvisorAdapterRegistry
to use.Default is the global
AdvisorAdapterRegistry
.
setCustomTargetSourceCreators
public void setCustomTargetSourceCreators(TargetSourceCreator... targetSourceCreators)
Set customTargetSourceCreators
to be applied in this order. If the list is empty, or they all return null, aSingletonTargetSource
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 aTargetSource
for a specific bean, that bean will be proxied in any case.TargetSourceCreators
can only be invoked if this post processor is used in aBeanFactory
and itsBeanFactoryAware
callback is triggered.- 参数:
targetSourceCreators
- the list ofTargetSourceCreators
. Ordering is significant: TheTargetSource
returned from the first matchingTargetSourceCreator
(that is, the first that returns non-null) will be used.
setInterceptorNames
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.
setApplyCommonInterceptorsFirst
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
public void setBeanFactory(BeanFactory beanFactory)
从接口复制的说明:BeanFactoryAware
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
InitializingBean.afterPropertiesSet()
or a custom init-method.- 指定者:
setBeanFactory
在接口中BeanFactoryAware
- 参数:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- 另请参阅:
BeanInitializationException
getBeanFactory
protected BeanFactory getBeanFactory()
Return the owningBeanFactory
. May benull
, as this post-processor doesn't need to belong to a bean factory.
predictBeanType
public Class<?> predictBeanType(Class<?> beanClass, String beanName)
从接口复制的说明:SmartInstantiationAwareBeanPostProcessor
Predict the type of the bean to be eventually returned from this processor'sInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
callback.- 指定者:
predictBeanType
在接口中SmartInstantiationAwareBeanPostProcessor
- 参数:
beanClass
- the raw class of the beanbeanName
- the name of the bean- 返回:
- the type of the bean, or
null
if not predictable
determineCandidateConstructors
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException
从接口复制的说明:SmartInstantiationAwareBeanPostProcessor
Determine the candidate constructors to use for the given bean.- 指定者:
determineCandidateConstructors
在接口中SmartInstantiationAwareBeanPostProcessor
- 参数:
beanClass
- the raw class of the bean (nevernull
)beanName
- the name of the bean- 返回:
- the candidate constructors, or
null
if none specified - 抛出:
BeansException
- in case of errors
getEarlyBeanReference
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException
从接口复制的说明:SmartInstantiationAwareBeanPostProcessor
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
BeanPostProcessor.postProcessBeforeInitialization(java.lang.Object, java.lang.String)
/BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.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).- 指定者:
getEarlyBeanReference
在接口中SmartInstantiationAwareBeanPostProcessor
- 参数:
bean
- the raw bean instancebeanName
- the name of the bean- 返回:
- the object to expose as bean reference (typically with the passed-in bean instance as default)
- 抛出:
BeansException
- in case of errors
postProcessBeforeInstantiation
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException
从接口复制的说明:InstantiationAwareBeanPostProcessor
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
BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String)
callback from the configuredBeanPostProcessors
.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
SmartInstantiationAwareBeanPostProcessor
interface in order to predict the type of the bean object that they are going to return here.- 指定者:
postProcessBeforeInstantiation
在接口中InstantiationAwareBeanPostProcessor
- 参数:
beanClass
- the class of the bean to be instantiatedbeanName
- the name of the bean- 返回:
- the bean object to expose instead of a default instance of the target bean, or
null
to proceed with default instantiation - 抛出:
BeansException
- in case of errors- 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation(java.lang.Object, java.lang.String)
,AbstractBeanDefinition.getBeanClass()
,AbstractBeanDefinition.getFactoryMethodName()
postProcessAfterInstantiation
public boolean postProcessAfterInstantiation(Object bean, String beanName)
从接口复制的说明:InstantiationAwareBeanPostProcessor
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 custom field injection on the given bean instance, right before Spring's autowiring kicks in.
- 指定者:
postProcessAfterInstantiation
在接口中InstantiationAwareBeanPostProcessor
- 参数:
bean
- the bean instance created, with properties not having been set yetbeanName
- the name of the bean- 返回:
true
if properties should be set on the bean;false
if property population should be skipped. Normal implementations should returntrue
. Returningfalse
will also prevent any subsequent InstantiationAwareBeanPostProcessor instances being invoked on this bean instance.- 另请参阅:
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
postProcessPropertyValues
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
从接口复制的说明:InstantiationAwareBeanPostProcessor
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.
- 指定者:
postProcessPropertyValues
在接口中InstantiationAwareBeanPostProcessor
- 参数:
pvs
- the property values that the factory is about to apply (nevernull
)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 setbeanName
- the name of the bean- 返回:
- the actual property values to apply to the given bean (can be the passed-in PropertyValues instance), or
null
to skip property population - 另请参阅:
MutablePropertyValues
postProcessBeforeInitialization
public Object postProcessBeforeInitialization(Object bean, String beanName)
从接口复制的说明:BeanPostProcessor
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSet
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.- 指定者:
postProcessBeforeInitialization
在接口中BeanPostProcessor
- 参数:
bean
- the new bean instancebeanName
- the name of the bean- 返回:
- the bean instance to use, either the original or a wrapped one; if
null
, no subsequent BeanPostProcessors will be invoked - 另请参阅:
InitializingBean.afterPropertiesSet()
postProcessAfterInitialization
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.- 指定者:
postProcessAfterInitialization
在接口中BeanPostProcessor
- 参数:
bean
- the new bean instancebeanName
- the name of the bean- 返回:
- the bean instance to use, either the original or a wrapped one; if
null
, no subsequent BeanPostProcessors will be invoked - 抛出:
BeansException
- in case of errors- 另请参阅:
getAdvicesAndAdvisorsForBean(java.lang.Class<?>, java.lang.String, org.springframework.aop.TargetSource)
getCacheKey
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
BeanFactory.FACTORY_BEAN_PREFIX
in case of aFactoryBean
; or if no bean name specified, then the given beanClass
as-is.- 参数:
beanClass
- the bean classbeanName
- the bean name- 返回:
- the cache key for the given class and name
wrapIfNecessary
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey)
Wrap the given bean if necessary, i.e. if it is eligible for being proxied.- 参数:
bean
- the raw bean instancebeanName
- the name of the beancacheKey
- the cache key for metadata access- 返回:
- a proxy wrapping the bean, or the raw bean instance as-is
isInfrastructureClass
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.
- 参数:
beanClass
- the class of the bean- 返回:
- whether the bean represents an infrastructure class
- 另请参阅:
Advice
,Advisor
,AopInfrastructureBean
,shouldSkip(java.lang.Class<?>, java.lang.String)
shouldSkip
protected boolean shouldSkip(Class<?> beanClass, String beanName)
Subclasses should override this method to returntrue
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 bean- 返回:
- whether to skip the given bean
getCustomTargetSource
protected TargetSource getCustomTargetSource(Class<?> beanClass, String beanName)
Create a target source for bean instances. Uses any TargetSourceCreators if set. Returnsnull
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 bean- 返回:
- a TargetSource for this bean
- 另请参阅:
setCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.TargetSourceCreator...)
createProxy
protected Object createProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource)
Create an AOP proxy for the given bean.- 参数:
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 bean- 返回:
- the AOP proxy for the bean
- 另请参阅:
buildAdvisors(java.lang.String, java.lang.Object[])
shouldProxyTargetClass
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
"preserveTargetClass" attribute
of the corresponding bean definition.- 参数:
beanClass
- the class of the beanbeanName
- the name of the bean- 返回:
- whether the given bean should be proxied with its target class
- 另请参阅:
AutoProxyUtils.shouldProxyTargetClass(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.lang.String)
advisorsPreFiltered
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.- 返回:
- whether the Advisors are pre-filtered
- 另请参阅:
getAdvicesAndAdvisorsForBean(java.lang.Class<?>, java.lang.String, org.springframework.aop.TargetSource)
,Advised.setPreFiltered(boolean)
buildAdvisors
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.- 参数:
beanName
- the name of the beanspecificInterceptors
- the set of interceptors that is specific to this bean (may be empty, but not null)- 返回:
- the list of Advisors for the given bean
customizeProxyFactory
protected void customizeProxyFactory(ProxyFactory proxyFactory)
Subclasses may choose to implement this: for example, to change the interfaces exposed.The default implementation is empty.
- 参数:
proxyFactory
- a ProxyFactory that is already configured with TargetSource and interfaces and will be used to create the proxy immediately after this method returns
getAdvicesAndAdvisorsForBean
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.- 参数:
beanClass
- the class of the bean to advisebeanName
- the name of the beancustomTargetSource
- the TargetSource returned by thegetCustomTargetSource(java.lang.Class<?>, java.lang.String)
method: may be ignored. Will benull
if no custom target source is in use.- 返回:
- 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. - 抛出:
BeansException
- in case of errors- 另请参阅:
DO_NOT_PROXY
,PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS