Class AbstractAutoProxyCreator

All Implemented Interfaces:
Serializable, AopInfrastructureBean, org.springframework.beans.factory.Aware, 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
Direct Known Subclasses:
AbstractAdvisorAutoProxyCreator, BeanNameAutoProxyCreator

public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor, org.springframework.beans.factory.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 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, Sam Brannen
See Also:
  • Field Details

  • Constructor Details

    • AbstractAutoProxyCreator

      public AbstractAutoProxyCreator()
  • Method Details

    • setFrozen

      public void setFrozen(boolean frozen)
      Set whether the proxy should be frozen, preventing advice from being added to it once it is created.

      Overridden from the superclass to prevent the proxy configuration from being frozen before the proxy is created.

      Overrides:
      setFrozen in class ProxyConfig
    • isFrozen

      public boolean isFrozen()
      Description copied from class: ProxyConfig
      Return whether the config is frozen, and no advice changes can be made.
      Overrides:
      isFrozen in class ProxyConfig
    • setAdvisorAdapterRegistry

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

      Default is the global AdvisorAdapterRegistry.

      See Also:
    • 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 triggered.

      Parameters:
      targetSourceCreators - the list of TargetSourceCreators. 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(org.springframework.beans.factory.BeanFactory beanFactory)
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
    • getBeanFactory

      @Nullable protected org.springframework.beans.factory.BeanFactory getBeanFactory()
      Return the owning BeanFactory. May be null, as this post-processor doesn't need to belong to a bean factory.
    • predictBeanType

      @Nullable public Class<?> predictBeanType(Class<?> beanClass, String beanName)
      Specified by:
      predictBeanType in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
    • determineBeanType

      public Class<?> determineBeanType(Class<?> beanClass, String beanName)
      Specified by:
      determineBeanType in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
    • determineCandidateConstructors

      @Nullable public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
      Specified by:
      determineCandidateConstructors in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
    • getEarlyBeanReference

      public Object getEarlyBeanReference(Object bean, String beanName)
      Specified by:
      getEarlyBeanReference in interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
    • postProcessBeforeInstantiation

      @Nullable public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName)
      Specified by:
      postProcessBeforeInstantiation in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
    • postProcessProperties

      public org.springframework.beans.PropertyValues postProcessProperties(org.springframework.beans.PropertyValues pvs, Object bean, String beanName)
      Specified by:
      postProcessProperties in interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
    • postProcessAfterInitialization

      @Nullable public Object postProcessAfterInitialization(@Nullable Object bean, String beanName)
      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
      See Also:
    • getCacheKey

      protected Object getCacheKey(Class<?> beanClass, @Nullable 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 a FactoryBean; or if no bean name specified, then the given bean Class as-is.

      Parameters:
      beanClass - the bean class
      beanName - the bean name
      Returns:
      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.
      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

      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.

      Parameters:
      beanClass - the class of the bean
      Returns:
      whether the bean represents an infrastructure class
      See Also:
    • shouldSkip

      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.

      Sometimes we need to be able to avoid this happening, e.g. if it will lead to a circular reference or if the existing target instance needs to be preserved. This implementation returns false unless the bean name indicates an "original instance" according to AutowireCapableBeanFactory conventions.

      Parameters:
      beanClass - the class of the bean
      beanName - the name of the bean
      Returns:
      whether to skip the given bean
      See Also:
      • AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX
    • getCustomTargetSource

      @Nullable 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:
      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:
    • createProxy

      protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, 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:
    • shouldProxyTargetClass

      protected boolean shouldProxyTargetClass(Class<?> beanClass, @Nullable 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.

      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:
    • 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:
    • buildAdvisors

      protected Advisor[] buildAdvisors(@Nullable String beanName, @Nullable 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(ProxyFactory proxyFactory)
      Subclasses may choose to implement this: for example, to change the interfaces exposed.

      The default implementation is empty.

      Parameters:
      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

      @Nullable protected abstract Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, @Nullable 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: