Class AbstractAutoProxyCreator
- All Implemented Interfaces:
- Serializable,- AopInfrastructureBean,- Aware,- BeanClassLoaderAware,- BeanFactoryAware,- BeanPostProcessor,- InstantiationAwareBeanPostProcessor,- SmartInstantiationAwareBeanPostProcessor,- Ordered
- Direct Known Subclasses:
- AbstractAdvisorAutoProxyCreator,- BeanNameAutoProxyCreator
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, for example, 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 SummaryFieldsModifier and TypeFieldDescriptionprotected static final Object[]Convenience constant for subclasses: Return value for "do not proxy".protected final LogLogger available to subclasses.protected static final Object[]Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".Fields inherited from interface org.springframework.core.OrderedHIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected booleanReturn 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 ObjectcreateProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) Create an AOP proxy for the given bean.protected voidcustomizeProxyFactory(ProxyFactory proxyFactory) Subclasses may choose to implement this: for example, to change the interfaces exposed.Class<?>determineBeanType(Class<?> beanClass, String beanName) Determine the type of the bean to be eventually returned from this processor'sInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)callback.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 (for example, AOP Alliance interceptors) and advisors to apply.protected BeanFactoryReturn the owningBeanFactory.protected ObjectgetCacheKey(Class<?> beanClass, String beanName) Build a cache key for the given bean class and bean name.protected TargetSourcegetCustomTargetSource(Class<?> beanClass, String beanName) Create a target source for bean instances.getEarlyBeanReference(Object bean, String beanName) Obtain a reference for early access to the specified bean, typically for the purpose of resolving a circular reference.booleanisFrozen()Return whether the config is frozen, and no advice changes can be made.protected booleanisInfrastructureClass(Class<?> beanClass) Return whether the given bean class represents an infrastructure class that should never be proxied.postProcessAfterInitialization(Object bean, String beanName) Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.postProcessBeforeInstantiation(Class<?> beanClass, String beanName) Apply this BeanPostProcessor before the target bean gets instantiated.postProcessProperties(PropertyValues pvs, 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.voidsetAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) Specify theAdvisorAdapterRegistryto use.voidsetApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst) Set whether the common interceptors should be applied before bean-specific ones.voidsetBeanFactory(BeanFactory beanFactory) Callback that supplies the owning factory to a bean instance.voidsetCustomTargetSourceCreators(TargetSourceCreator... targetSourceCreators) Set customTargetSourceCreatorsto be applied in this order.voidsetFrozen(boolean frozen) Set whether the proxy should be frozen, preventing advice from being added to it once it is created.voidsetInterceptorNames(String... interceptorNames) Set the common interceptors.protected booleanshouldProxyTargetClass(Class<?> beanClass, String beanName) Determine whether the given bean should be proxied with its target class rather than its interfaces.protected booleanshouldSkip(Class<?> beanClass, String beanName) Subclasses should override this method to returntrueif the given bean should not be considered for auto-proxying by this post-processor.protected ObjectwrapIfNecessary(Object bean, String beanName, Object cacheKey) Wrap the given bean if necessary, i.e.Methods inherited from class org.springframework.aop.framework.ProxyProcessorSupportevaluateProxyInterfaces, getOrder, getProxyClassLoader, isConfigurationCallbackInterface, isInternalLanguageInterface, setBeanClassLoader, setOrder, setProxyClassLoaderMethods inherited from class org.springframework.aop.framework.ProxyConfigcopyFrom, isExposeProxy, isOpaque, isOptimize, isProxyTargetClass, setExposeProxy, setOpaque, setOptimize, setProxyTargetClass, toStringMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.beans.factory.config.BeanPostProcessorpostProcessBeforeInitializationMethods inherited from interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorpostProcessAfterInstantiation
- 
Field Details- 
DO_NOT_PROXYConvenience constant for subclasses: Return value for "do not proxy".
- 
PROXY_WITHOUT_ADDITIONAL_INTERCEPTORSConvenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".
- 
loggerLogger available to subclasses.
 
- 
- 
Constructor Details- 
AbstractAutoProxyCreatorpublic AbstractAutoProxyCreator()
 
- 
- 
Method Details- 
setFrozenpublic 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:
- setFrozenin class- ProxyConfig
 
- 
isFrozenpublic boolean isFrozen()Description copied from class:ProxyConfigReturn whether the config is frozen, and no advice changes can be made.- Overrides:
- isFrozenin class- ProxyConfig
 
- 
setAdvisorAdapterRegistrySpecify theAdvisorAdapterRegistryto use.Default is the global AdvisorAdapterRegistry.- See Also:
 
- 
setCustomTargetSourceCreatorsSet customTargetSourceCreatorsto be applied in this order. If the list is empty, or they all return null, aSingletonTargetSourcewill 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 TargetSourceCreatorreturns aTargetSourcefor a specific bean, that bean will be proxied in any case.TargetSourceCreatorscan only be invoked if this post processor is used in aBeanFactoryand itsBeanFactoryAwarecallback is triggered.- Parameters:
- targetSourceCreators- the list of- TargetSourceCreators. Ordering is significant: The- TargetSourcereturned from the first matching- TargetSourceCreator(that is, the first that returns non-null) will be used.
 
- 
setInterceptorNamesSet 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. 
- 
setApplyCommonInterceptorsFirstpublic 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.
- 
setBeanFactoryDescription copied from interface:BeanFactoryAwareCallback 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.- Specified by:
- setBeanFactoryin interface- BeanFactoryAware
- Parameters:
- beanFactory- owning BeanFactory (never- null). The bean can immediately call methods on the factory.
- See Also:
 
- 
getBeanFactoryReturn the owningBeanFactory. May benull, as this post-processor doesn't need to belong to a bean factory.
- 
predictBeanTypeDescription copied from interface:SmartInstantiationAwareBeanPostProcessorPredict the type of the bean to be eventually returned from this processor'sInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)callback.The default implementation returns null. Specific implementations should try to predict the bean type as far as known/cached already, without extra processing steps.- Specified by:
- predictBeanTypein interface- SmartInstantiationAwareBeanPostProcessor
- Parameters:
- beanClass- the raw class of the bean
- beanName- the name of the bean
- Returns:
- the type of the bean, or nullif not predictable
 
- 
determineBeanTypeDescription copied from interface:SmartInstantiationAwareBeanPostProcessorDetermine the type of the bean to be eventually returned from this processor'sInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)callback.The default implementation returns the given bean class as-is. Specific implementations should fully evaluate their processing steps in order to create/initialize a potential proxy class upfront. - Specified by:
- determineBeanTypein interface- SmartInstantiationAwareBeanPostProcessor
- Parameters:
- beanClass- the raw class of the bean
- beanName- the name of the bean
- Returns:
- the type of the bean (never null)
 
- 
determineCandidateConstructors@Nullable public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) Description copied from interface:SmartInstantiationAwareBeanPostProcessorDetermine the candidate constructors to use for the given bean.The default implementation returns null.- Specified by:
- determineCandidateConstructorsin interface- SmartInstantiationAwareBeanPostProcessor
- Parameters:
- beanClass- the raw class of the bean (never- null)
- beanName- the name of the bean
- Returns:
- the candidate constructors, or nullif none specified
 
- 
getEarlyBeanReferenceDescription copied from interface:SmartInstantiationAwareBeanPostProcessorObtain 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 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 the 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 exposed as the final bean reference by default).The default implementation returns the given beanas-is.- Specified by:
- getEarlyBeanReferencein interface- SmartInstantiationAwareBeanPostProcessor
- Parameters:
- bean- the raw bean instance
- beanName- the name of the bean
- Returns:
- the object to expose as the bean reference (typically the passed-in bean instance as default)
 
- 
postProcessBeforeInstantiationDescription copied from interface:InstantiationAwareBeanPostProcessorApply 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 SmartInstantiationAwareBeanPostProcessorinterface in order to predict the type of the bean object that they are going to return here.The default implementation returns null.- Specified by:
- postProcessBeforeInstantiationin interface- InstantiationAwareBeanPostProcessor
- Parameters:
- beanClass- the class of the bean to be instantiated
- beanName- the name of the bean
- Returns:
- the bean object to expose instead of a default instance of the target bean,
 or nullto proceed with default instantiation
- See Also:
 
- 
postProcessPropertiesDescription copied from interface:InstantiationAwareBeanPostProcessorPost-process the given property values before the factory applies them to the given bean.The default implementation returns the given pvsas-is.- Specified by:
- postProcessPropertiesin interface- InstantiationAwareBeanPostProcessor
- Parameters:
- pvs- the property values that the factory is about to apply (never- null)
- bean- the bean instance created, but whose properties have not yet been set
- beanName- the name of the bean
- Returns:
- the actual property values to apply to the given bean (can be the passed-in
 PropertyValues instance), or nullto skip property population
 
- 
postProcessAfterInitializationCreate a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.- Specified by:
- postProcessAfterInitializationin interface- BeanPostProcessor
- Parameters:
- bean- the new bean instance
- beanName- the name of the bean
- Returns:
- the bean instance to use, either the original or a wrapped one;
 if null, no subsequent BeanPostProcessors will be invoked
- See Also:
 
- 
getCacheKeyBuild 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_PREFIXin case of aFactoryBean; or if no bean name specified, then the given beanClassas-is.- Parameters:
- beanClass- the bean class
- beanName- the bean name
- Returns:
- the cache key for the given class and name
 
- 
wrapIfNecessaryWrap 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
 
- 
isInfrastructureClassReturn 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:
 
- 
shouldSkipSubclasses should override this method to returntrueif the given bean should not be considered for auto-proxying by this post-processor.Sometimes we need to be able to avoid this happening, for example, if it will lead to a circular reference or if the existing target instance needs to be preserved. This implementation returns falseunless the bean name indicates an "original instance" according toAutowireCapableBeanFactoryconventions.- Parameters:
- beanClass- the class of the bean
- beanName- the name of the bean
- Returns:
- whether to skip the given bean
- See Also:
 
- 
getCustomTargetSourceCreate a target source for bean instances. Uses any TargetSourceCreators if set. Returnsnullif 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:
 
- 
createProxyprotected 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:
 
- 
shouldProxyTargetClassDetermine whether the given bean should be proxied with its target class rather than its interfaces.Checks the "preserveTargetClass" attributeof 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:
 
- 
advisorsPreFilteredprotected 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:
 
- 
buildAdvisorsprotected 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
 
- 
customizeProxyFactorySubclasses 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 BeansException Return whether the given bean is to be proxied, what additional advices (for example, 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- nullif 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 nullif 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:
 
 
-