org.springframework.aop.framework.autoproxy
Class AbstractAutoProxyCreator

java.lang.Object
  extended by org.springframework.aop.framework.ProxyConfig
      extended by org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
All Implemented Interfaces:
java.io.Serializable, org.springframework.aop.framework.AopInfrastructureBean, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.config.BeanPostProcessor, org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor, org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor, org.springframework.core.Ordered

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.

Since:
13.10.2003
Author:
Juergen Hoeller, Rod Johnson, Rob Harrop
See Also:
setInterceptorNames(java.lang.String[]), getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource), BeanNameAutoProxyCreator, DefaultAdvisorAutoProxyCreator, Serialized Form

Field Summary
protected static java.lang.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 java.lang.Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
          Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
AbstractAutoProxyCreator()
           
 
Method 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.
protected  org.springframework.aop.Advisor[] buildAdvisors(java.lang.String beanName, java.lang.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  java.lang.Object createProxy(java.lang.Class beanClass, java.lang.String beanName, java.lang.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.
 java.lang.reflect.Constructor[] determineCandidateConstructors(java.lang.Class beanClass, java.lang.String beanName)
           
protected abstract  java.lang.Object[] getAdvicesAndAdvisorsForBean(java.lang.Class beanClass, java.lang.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  java.lang.Object getCacheKey(java.lang.Class beanClass, java.lang.String beanName)
          Build a cache key for the given bean class and bean name.
protected  org.springframework.aop.TargetSource getCustomTargetSource(java.lang.Class beanClass, java.lang.String beanName)
          Create a target source for bean instances.
 java.lang.Object getEarlyBeanReference(java.lang.Object bean, java.lang.String beanName)
           
 int getOrder()
           
 boolean isFrozen()
           
protected  boolean isInfrastructureClass(java.lang.Class beanClass)
          Return whether the given bean class represents an infrastructure class that should never be proxied.
protected  boolean isInfrastructureClass(java.lang.Class beanClass, java.lang.String beanName)
          Deprecated. since 4.2 Use isInfrastructureClass(Class)
 java.lang.Object postProcessAfterInitialization(java.lang.Object bean, java.lang.String beanName)
          Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.
 boolean postProcessAfterInstantiation(java.lang.Object bean, java.lang.String beanName)
           
 java.lang.Object postProcessBeforeInitialization(java.lang.Object bean, java.lang.String beanName)
           
 java.lang.Object postProcessBeforeInstantiation(java.lang.Class beanClass, java.lang.String beanName)
           
 org.springframework.beans.PropertyValues postProcessPropertyValues(org.springframework.beans.PropertyValues pvs, java.beans.PropertyDescriptor[] pds, java.lang.Object bean, java.lang.String beanName)
           
 java.lang.Class predictBeanType(java.lang.Class beanClass, java.lang.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(java.lang.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(java.lang.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(java.lang.ClassLoader classLoader)
          Set the ClassLoader to generate the proxy class in.
protected  boolean shouldProxyTargetClass(java.lang.Class beanClass, java.lang.String beanName)
          Determine whether the given bean should be proxied with its target class rather than its interfaces.
protected  boolean shouldSkip(java.lang.Class beanClass, java.lang.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  java.lang.Object wrapIfNecessary(java.lang.Object bean, java.lang.String beanName, java.lang.Object cacheKey)
          Wrap the given bean if necessary, i.e.
 
Methods inherited from class org.springframework.aop.framework.ProxyConfig
copyFrom, isExposeProxy, isOpaque, isOptimize, isProxyTargetClass, setExposeProxy, setOpaque, setOptimize, setProxyTargetClass, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DO_NOT_PROXY

protected static final java.lang.Object[] DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy".

See Also:
getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource)

PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS

protected static final java.lang.Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".

See Also:
getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource)

logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses

Constructor Detail

AbstractAutoProxyCreator

public AbstractAutoProxyCreator()
Method Detail

setOrder

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

getOrder

public final int getOrder()
Specified by:
getOrder in interface org.springframework.core.Ordered

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.

Overrides:
setFrozen in class org.springframework.aop.framework.ProxyConfig

isFrozen

public boolean isFrozen()
Overrides:
isFrozen in class org.springframework.aop.framework.ProxyConfig

setAdvisorAdapterRegistry

public void setAdvisorAdapterRegistry(org.springframework.aop.framework.adapter.AdvisorAdapterRegistry advisorAdapterRegistry)
Specify the AdvisorAdapterRegistry to use. Default is the global AdvisorAdapterRegistry.

See Also:
GlobalAdvisorAdapterRegistry

setCustomTargetSourceCreators

public void setCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.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.

setInterceptorNames

public void setInterceptorNames(java.lang.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.


setProxyClassLoader

public void setProxyClassLoader(java.lang.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.


setBeanClassLoader

public void setBeanClassLoader(java.lang.ClassLoader classLoader)
Specified by:
setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware

setBeanFactory

public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
Specified by:
setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware

getBeanFactory

protected org.springframework.beans.factory.BeanFactory getBeanFactory()
Return the owning BeanFactory. May be null, as this object doesn't need to belong to a bean factory.


predictBeanType

public java.lang.Class predictBeanType(java.lang.Class beanClass,
                                       java.lang.String beanName)
Specified by:
predictBeanType in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor

determineCandidateConstructors

public java.lang.reflect.Constructor[] determineCandidateConstructors(java.lang.Class beanClass,
                                                                      java.lang.String beanName)
                                                               throws org.springframework.beans.BeansException
Specified by:
determineCandidateConstructors in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
Throws:
org.springframework.beans.BeansException

getEarlyBeanReference

public java.lang.Object getEarlyBeanReference(java.lang.Object bean,
                                              java.lang.String beanName)
                                       throws org.springframework.beans.BeansException
Specified by:
getEarlyBeanReference in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
Throws:
org.springframework.beans.BeansException

postProcessBeforeInstantiation

public java.lang.Object postProcessBeforeInstantiation(java.lang.Class beanClass,
                                                       java.lang.String beanName)
                                                throws org.springframework.beans.BeansException
Specified by:
postProcessBeforeInstantiation in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
Throws:
org.springframework.beans.BeansException

postProcessAfterInstantiation

public boolean postProcessAfterInstantiation(java.lang.Object bean,
                                             java.lang.String beanName)
Specified by:
postProcessAfterInstantiation in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor

postProcessPropertyValues

public org.springframework.beans.PropertyValues postProcessPropertyValues(org.springframework.beans.PropertyValues pvs,
                                                                          java.beans.PropertyDescriptor[] pds,
                                                                          java.lang.Object bean,
                                                                          java.lang.String beanName)
Specified by:
postProcessPropertyValues in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor

postProcessBeforeInitialization

public java.lang.Object postProcessBeforeInitialization(java.lang.Object bean,
                                                        java.lang.String beanName)
Specified by:
postProcessBeforeInitialization in interface org.springframework.beans.factory.config.BeanPostProcessor

postProcessAfterInitialization

public java.lang.Object postProcessAfterInitialization(java.lang.Object bean,
                                                       java.lang.String beanName)
                                                throws org.springframework.beans.BeansException
Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.

Specified by:
postProcessAfterInitialization in interface org.springframework.beans.factory.config.BeanPostProcessor
Throws:
org.springframework.beans.BeansException
See Also:
getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource)

getCacheKey

protected java.lang.Object getCacheKey(java.lang.Class beanClass,
                                       java.lang.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

wrapIfNecessary

protected java.lang.Object wrapIfNecessary(java.lang.Object bean,
                                           java.lang.String beanName,
                                           java.lang.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

isInfrastructureClass

@Deprecated
protected boolean isInfrastructureClass(java.lang.Class beanClass,
                                                   java.lang.String beanName)
Deprecated. since 4.2 Use isInfrastructureClass(Class)

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


isInfrastructureClass

protected boolean isInfrastructureClass(java.lang.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:
Advisor, MethodInterceptor, shouldSkip(java.lang.Class, java.lang.String)

shouldSkip

protected boolean shouldSkip(java.lang.Class beanClass,
                             java.lang.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

getCustomTargetSource

protected org.springframework.aop.TargetSource getCustomTargetSource(java.lang.Class beanClass,
                                                                     java.lang.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
See Also:
setCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.TargetSourceCreator[])

createProxy

protected java.lang.Object createProxy(java.lang.Class beanClass,
                                       java.lang.String beanName,
                                       java.lang.Object[] specificInterceptors,
                                       org.springframework.aop.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
See Also:
buildAdvisors(java.lang.String, java.lang.Object[])

shouldProxyTargetClass

protected boolean shouldProxyTargetClass(java.lang.Class beanClass,
                                         java.lang.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
See Also:
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.

Returns:
whether the Advisors are pre-filtered
See Also:
getAdvicesAndAdvisorsForBean(java.lang.Class, java.lang.String, org.springframework.aop.TargetSource), Advised.setPreFiltered(boolean)

buildAdvisors

protected org.springframework.aop.Advisor[] buildAdvisors(java.lang.String beanName,
                                                          java.lang.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

customizeProxyFactory

protected void customizeProxyFactory(org.springframework.aop.framework.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

getAdvicesAndAdvisorsForBean

protected abstract java.lang.Object[] getAdvicesAndAdvisorsForBean(java.lang.Class beanClass,
                                                                   java.lang.String beanName,
                                                                   org.springframework.aop.TargetSource customTargetSource)
                                                            throws org.springframework.beans.BeansException
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(java.lang.Class, java.lang.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:
org.springframework.beans.BeansException - in case of errors
See Also:
DO_NOT_PROXY, PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS


Copyright © 2003-2014 Atlassian. All Rights Reserved.