org.springframework.aop.framework.autoproxy
Class AbstractAutoProxyCreator

public class AbstractAutoProxyCreator
extends ProxyConfig
implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Ordered
BeanPostProcessor implementation that wraps a group of beans with AOP proxies that delegate to the given 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 with any subclass, to create a custom target source - for example, to pool prototype objects. Autoproxying will occur even if there is no advice if 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 bean to be autoproxied.

Since13.10.2003
VersionNot specified.
AuthorJuergen Hoeller, Rod Johnson, Rob Harrop
Wiki javadoc Use textile entry format.
Add your comments here.
Field Summary
protected static Object[] DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy".
protected static Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".
Fields inherited from org.springframework.aop.frameworkProxyConfig
Constructor Summary
AbstractAutoProxyCreator()
No description provided.
Method Summary
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.
protected 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 owning BeanFactory May be null, as this object doesn't need to belong to a bean factory.
protected TargetSource getCustomTargetSource( Class beanClass, String beanName )
Create a target source for bean instances.
int getOrder()
No description provided.
boolean isFrozen()
No description provided.
protected boolean isInfrastructureClass( Class beanClass, String beanName )
Return whether the given bean class and bean name 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.
Object postProcessBeforeInitialization( Object bean, String beanName )
No description provided.
Object postProcessBeforeInstantiation( Class beanClass, String beanName )
No description provided.
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 setBeanFactory( BeanFactory beanFactory )
No description provided.
void setCustomTargetSourceCreators( TargetSourceCreator [] targetSourceCreators )
Set custom TargetSourceCreators to be applied in this order.
void setFrozen( boolean frozen )
Sets 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.
protected boolean shouldSkip( Class beanClass, String beanName )
Subclasses should override this method to return true if this bean should not be considered for auto-proxying by this post processor.
DO_NOT_PROXY
protected static Object[] DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy".
Wiki javadoc Use textile entry format.
Add your comments here.
PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
protected static Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".
Wiki javadoc Use textile entry format.
Add your comments here.
AbstractAutoProxyCreator
public AbstractAutoProxyCreator ( )
No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
createProxy
protected Object createProxy ( Class beanClass, String beanName, Object [] specificInterceptors, TargetSource targetSource )
Create an AOP proxy for the given bean.
Parameters
TypeNameDescription
Class beanClass the class of the bean
String beanName the name of the bean
Object [] specificInterceptors the set of interceptors that is specific to this bean (may be empty, but not null)
TargetSource targetSource the TargetSource for the proxy, already pre-configured to access the bean
Wiki javadoc Use textile entry format.
Add your comments here.
customizeProxyFactory
protected void customizeProxyFactory ( ProxyFactory proxyFactory )
Subclasses may choose to implement this: for example, to change the interfaces exposed.

Default implementation is emty.

Parameters
TypeNameDescription
ProxyFactory proxyFactory ProxyFactory that is already configured with TargetSource and interfaces and will be used to create the proxy immediably after this method returns
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
getAdvicesAndAdvisorsForBean
protected 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.

The previous name of this method was "getInterceptorAndAdvisorForBean". It has been renamed in the course of general terminology clarification in Spring 1.1. An AOP Alliance Interceptor is just a special form of Advice, so the generic Advice term is preferred now.

The third parameter, customTargetSource, is new in Spring 1.1; add it to existing implementations of this method.

Parameters
TypeNameDescription
Class beanClass the class of the bean to advise
String beanName the name of the bean
TargetSource customTargetSource targetSource returned by getTargetSource() method: may be ignored. Will be null unless a custom target source is in use.
Exceptions
BeansException in case of errors
Wiki javadoc Use textile entry format.
Add your comments here.
getBeanFactory
protected BeanFactory getBeanFactory ( )
Return the owning BeanFactory May be null, as this object doesn't need to belong to a bean factory.
Wiki javadoc Use textile entry format.
Add your comments here.
getCustomTargetSource
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
TypeNameDescription
Class beanClass the class of the bean to create a TargetSource for
String beanName the name of the bean
Wiki javadoc Use textile entry format.
Add your comments here.
getOrder
public int getOrder ( )
No description provided.
Implements method in Ordered
Wiki javadoc Use textile entry format.
Add your comments here.
isFrozen
public boolean isFrozen ( )
No description provided.
Overrides method in ProxyConfig
Wiki javadoc Use textile entry format.
Add your comments here.
isInfrastructureClass
protected boolean isInfrastructureClass ( Class beanClass, String beanName )
Return whether the given bean class and bean name represents an infrastructure class that should never be proxied.

Default implementation considers Advisors, MethodInterceptors and AbstractAutoProxyCreators as infrastructure classes, and consults the shouldSkip method.

Parameters
TypeNameDescription
Class beanClass the class of the bean
String beanName the name of the bean
Wiki javadoc Use textile entry format.
Add your comments here.
postProcessAfterInitialization
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.
Implements method in BeanPostProcessor
Parameters
TypeNameDescription
Object bean No description provided.
String beanName No description provided.
Exceptions
BeansException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
postProcessBeforeInitialization
public Object postProcessBeforeInitialization ( Object bean, String beanName )
No description provided.
Implements method in BeanPostProcessor
Parameters
TypeNameDescription
Object bean No description provided.
String beanName No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
postProcessBeforeInstantiation
public Object postProcessBeforeInstantiation ( Class beanClass, String beanName )
No description provided.
Parameters
TypeNameDescription
Class beanClass No description provided.
String beanName No description provided.
Exceptions
BeansException No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setAdvisorAdapterRegistry
public void setAdvisorAdapterRegistry ( AdvisorAdapterRegistry advisorAdapterRegistry )
Specify the AdvisorAdapterRegistry to use. Default is the global AdvisorAdapterRegistry.
Parameters
TypeNameDescription
AdvisorAdapterRegistry advisorAdapterRegistry No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
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.
Parameters
TypeNameDescription
boolean applyCommonInterceptorsFirst No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setBeanFactory
public void setBeanFactory ( BeanFactory beanFactory )
No description provided.
Implements method in BeanFactoryAware
Parameters
TypeNameDescription
BeanFactory beanFactory No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setCustomTargetSourceCreators
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
TypeNameDescription
TargetSourceCreator [] 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.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setFrozen
public void setFrozen ( boolean frozen )
Sets 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 method in ProxyConfig
Parameters
TypeNameDescription
boolean frozen No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
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.
Parameters
TypeNameDescription
String [] interceptorNames No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setOrder
public 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
TypeNameDescription
int order ordering value
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
shouldSkip
protected boolean shouldSkip ( Class beanClass, String beanName )
Subclasses should override this method to return true if this 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
TypeNameDescription
Class beanClass the class of the bean
String beanName the name of the bean
Wiki javadoc Use textile entry format.
Add your comments here.