org.springframework.aop.framework.autoproxy
Class AbstractAutoProxyCreator

java.lang.Object
  extended byorg.springframework.aop.framework.ProxyConfig
      extended byorg.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
All Implemented Interfaces:
BeanFactoryAware, BeanPostProcessor, Ordered, Serializable
Direct Known Subclasses:
AbstractAdvisorAutoProxyCreator, BeanNameAutoProxyCreator

public abstract class AbstractAutoProxyCreator
extends ProxyConfig
implements BeanPostProcessor, 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 autoproxying 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.

Since:
October 13, 2003
Author:
Juergen Hoeller, Rod Johnson
See Also:
setInterceptorNames(java.lang.String[]), BeanNameAutoProxyCreator, Serialized Form

Field Summary
protected static Object[] DO_NOT_PROXY
          Convenience constant for subclasses: Return value for "do not proxy".
protected  Log logger
           
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 class org.springframework.aop.framework.ProxyConfig
exposeProxy
 
Constructor Summary
AbstractAutoProxyCreator()
           
 
Method Summary
protected  void customizeProxyFactory(Object bean, ProxyFactory proxyFactory)
          Subclasses may choose to implement this: for example, to change the interfaces exposed
protected abstract  Object[] getAdvicesAndAdvisorsForBean(Object bean, 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(Object bean, String beanName)
          Create a target source for bean instances.
 int getOrder()
          Return the order value of this object, higher value meaning greater in terms of sorting.
protected  boolean isInfrastructureClass(Object bean, String beanName)
           
 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)
          Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).
 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)
          Callback that supplies the owning factory to a bean instance.
 void setCustomTargetSourceCreators(List targetSourceCreators)
          Set custom TargetSourceCreators to be applied in this order.
 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(Object bean, String beanName)
          Subclasses should override this method to return true if this bean should not be considered for autoproxying by this post processor.
 
Methods inherited from class org.springframework.aop.framework.ProxyConfig
copyFrom, getAopProxyFactory, getExposeProxy, getOpaque, getOptimize, getProxyTargetClass, isExposeProxy, isFrozen, isOpaque, isOptimize, isProxyTargetClass, setAopProxyFactory, setExposeProxy, setFrozen, 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 Object[] DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy".

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

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".

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

logger

protected final Log logger
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()
Description copied from interface: Ordered
Return the order value of this object, higher value meaning greater in terms of sorting. Normally starting with 0 or 1, Integer.MAX_VALUE indicating greatest. Same order values will result in arbitrary positions for the affected objects.

Higher value can be interpreted as lower priority, consequently the first object has highest priority (somewhat analogous to Servlet "load-on-startup" values).

Specified by:
getOrder in interface Ordered
Returns:
the order value

setAdvisorAdapterRegistry

public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)
Specify the AdvisorAdapterRegistry to use. Default is the global AdvisorAdapterRegistry.

See Also:
GlobalAdvisorAdapterRegistry

setCustomTargetSourceCreators

public void setCustomTargetSourceCreators(List 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.

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(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)
Description copied from interface: BeanFactoryAware
Callback that supplies the owning factory to a bean instance.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (may not be null). The bean can immediately call methods on the factory.
See Also:
BeanInitializationException

getBeanFactory

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


postProcessBeforeInitialization

public Object postProcessBeforeInitialization(Object bean,
                                              String beanName)
Description copied from interface: BeanPostProcessor
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.

Specified by:
postProcessBeforeInitialization in interface BeanPostProcessor
Parameters:
bean - the new bean instance
beanName - the beanName of the bean
Returns:
the bean instance to use, either the original or a wrapped one
See Also:
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.

Specified by:
postProcessAfterInitialization in interface BeanPostProcessor
Parameters:
bean - the new bean instance
beanName - the beanName of the bean
Returns:
the bean instance to use, either the original or a wrapped one
Throws:
BeansException - in case of errors
See Also:
getAdvicesAndAdvisorsForBean(java.lang.Object, java.lang.String, org.springframework.aop.TargetSource)

customizeProxyFactory

protected void customizeProxyFactory(Object bean,
                                     ProxyFactory proxyFactory)
Subclasses may choose to implement this: for example, to change the interfaces exposed

Parameters:
bean - bean about to be autoproxied
proxyFactory - ProxyFactory that will be used to create the proxy immediably after this method returns

isInfrastructureClass

protected boolean isInfrastructureClass(Object bean,
                                        String beanName)

shouldSkip

protected boolean shouldSkip(Object bean,
                             String beanName)
Subclasses should override this method to return true if this bean should not be considered for autoproxying 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:
bean - the new bean instance
beanName - the name of the bean

getCustomTargetSource

protected TargetSource getCustomTargetSource(Object bean,
                                             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:
bean - the new bean instance
beanName - the name of the bean
Returns:
a TargetSource for this bean
See Also:
setCustomTargetSourceCreators(java.util.List)

getAdvicesAndAdvisorsForBean

protected abstract Object[] getAdvicesAndAdvisorsForBean(Object bean,
                                                         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.

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:
bean - the new bean instance
beanName - the name of the bean
customTargetSource - targetSource returned by getTargetSource() method: may be ignored. Will be null unless a 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
See Also:
postProcessAfterInitialization(java.lang.Object, java.lang.String), DO_NOT_PROXY, PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS


Copyright (C) 2003-2004 The Spring Framework Project.