类 AbstractAutoProxyCreator
- 所有已实现的接口:
AopInfrastructureBean,ProxyCreator,infra.beans.factory.Aware,infra.beans.factory.BeanClassLoaderAware,infra.beans.factory.BeanFactoryAware,infra.beans.factory.config.BeanPostProcessor,infra.beans.factory.config.InstantiationAwareBeanPostProcessor,infra.beans.factory.config.SmartInstantiationAwareBeanPostProcessor,infra.beans.factory.InitializationBeanPostProcessor,infra.core.Ordered,Serializable
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.
- 从以下版本开始:
- 3.0 2021/2/1 21:31
- 作者:
- Juergen Hoeller, Rod Johnson, Rob Harrop, Harry Yang
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明private final ConcurrentHashMap<Object,Boolean> private AdvisorAdapterRegistryDefault is global AdvisorAdapterRegistry.private booleanprivate infra.beans.factory.BeanFactoryprivate TargetSourceCreator[]protected static final Object[]Convenience constant for subclasses: Return value for "do not proxy".private final ConcurrentHashMap<Object,Object> private booleanIndicates whether or not the proxy should be frozen.private String[]Default is no common interceptors.private static final infra.logging.Loggerprotected static final Object[]Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones".private final ConcurrentHashMap<Object,Class<?>> private static final long从接口继承的字段 infra.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected 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.private ObjectbuildProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource, boolean classOnly) protected ObjectcreateProxy(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) Create an AOP proxy for the given bean.private Class<?>createProxyClass(Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) protected voidcustomizeProxyFactory(ProxyFactory proxyFactory) Subclasses may choose to implement this: for example, to change the interfaces exposed.Class<?>determineBeanType(Class<?> beanClass, String beanName) Constructor<?>[]determineCandidateConstructors(Class<?> beanClass, String beanName) protected abstract Object[]getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource targetSource) Return whether the given bean is to be proxied, what additional advices (e.g.infra.beans.factory.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) getEarlyBeanReference(Object bean, String beanName) protected TargetSourcegetTargetSource(Object bean, String def) 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) Class<?>predictBeanType(Class<?> beanClass, String beanName) private Advisor[]Resolves the specified interceptor names to Advisor objects.voidsetAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) Specify theAdvisorAdapterRegistryto use.voidsetApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst) Set whether the common interceptors should be applied before bean-specific ones.voidsetBeanFactory(infra.beans.factory.BeanFactory beanFactory) voidsetCustomTargetSourceCreators(TargetSourceCreator... customTargetSourceCreators) Set customTargetSourceCreatorsto be applied in this order.voidsetFrozen(boolean frozen) Set whether or not 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. if it is eligible for being proxied.从类继承的方法 infra.aop.framework.ProxyProcessorSupport
evaluateProxyInterfaces, getOrder, getProxyClassLoader, isConfigurationCallbackInterface, isInternalLanguageInterface, setBeanClassLoader, setOrder, setProxyClassLoader从类继承的方法 infra.aop.framework.ProxyConfig
copyFrom, isExposeProxy, isOpaque, isOptimize, isProxyTargetClass, setExposeProxy, setOpaque, setOptimize, setProxyTargetClass, toString从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait从接口继承的方法 infra.beans.factory.InitializationBeanPostProcessor
postProcessBeforeInitialization从接口继承的方法 infra.beans.factory.config.InstantiationAwareBeanPostProcessor
postProcessAfterInstantiation
-
字段详细资料
-
serialVersionUID
private static final long serialVersionUID- 另请参阅:
-
log
private static final infra.logging.Logger log -
DO_NOT_PROXY
Convenience constant for subclasses: Return value for "do not proxy". -
PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
Convenience constant for subclasses: Return value for "proxy without additional interceptors, just the common ones". -
beanFactory
@Nullable private infra.beans.factory.BeanFactory beanFactory -
freezeProxy
private boolean freezeProxyIndicates whether or not the proxy should be frozen. Overridden from super to prevent the configuration from becoming frozen too early. -
customTargetSourceCreators
-
interceptorNames
Default is no common interceptors. -
applyCommonInterceptorsFirst
private boolean applyCommonInterceptorsFirst -
advisorAdapterRegistry
Default is global AdvisorAdapterRegistry. -
targetSourcedBeans
-
earlyBeanReferences
-
advisedBeans
-
proxyTypes
-
-
构造器详细资料
-
AbstractAutoProxyCreator
public AbstractAutoProxyCreator()
-
-
方法详细资料
-
setCustomTargetSourceCreators
Set 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.- 参数:
customTargetSourceCreators- the list ofTargetSourceCreators. Ordering is significant: TheTargetSourcereturned from the first matchingTargetSourceCreator(that is, the first that returns non-null) will be used.
-
setAdvisorAdapterRegistry
Specify theAdvisorAdapterRegistryto use.Default is the global
AdvisorAdapterRegistry. -
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.
- 覆盖:
setFrozen在类中ProxyConfig
-
isFrozen
public boolean isFrozen()从类复制的说明:ProxyConfigReturn whether the config is frozen, and no advice changes can be made.- 覆盖:
isFrozen在类中ProxyConfig
-
setInterceptorNames
Set the common interceptors. These must be bean names in the current factory. They can be of any advice or advisor type 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(infra.beans.factory.BeanFactory beanFactory) - 指定者:
setBeanFactory在接口中infra.beans.factory.BeanFactoryAware
-
getBeanFactory
@Nullable public infra.beans.factory.BeanFactory getBeanFactory()Return the owningBeanFactory. May benull, as this post-processor doesn't need to belong to a bean factory. -
postProcessBeforeInstantiation
- 指定者:
postProcessBeforeInstantiation在接口中infra.beans.factory.config.InstantiationAwareBeanPostProcessor
-
getCustomTargetSource
-
predictBeanType
- 指定者:
predictBeanType在接口中infra.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
-
determineBeanType
- 指定者:
determineBeanType在接口中infra.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
-
determineCandidateConstructors
@Nullable public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) - 指定者:
determineCandidateConstructors在接口中infra.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
-
getEarlyBeanReference
- 指定者:
getEarlyBeanReference在接口中infra.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
-
postProcessAfterInitialization
Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.- 指定者:
postProcessAfterInitialization在接口中infra.beans.factory.InitializationBeanPostProcessor- 另请参阅:
-
getCacheKey
Build a cache key for the given bean class and bean name.Note: 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.- 参数:
beanClass- the bean classbeanName- the bean name- 返回:
- the cache key for the given class and name
- 从以下版本开始:
- 4.0
-
wrapIfNecessary
Wrap the given bean if necessary, i.e. if it is eligible for being proxied.- 参数:
bean- the raw bean instancebeanName- the name of the beancacheKey- the cache key for metadata access- 返回:
- a proxy wrapping the bean, or the raw bean instance as-is
-
getTargetSource
-
createProxy
protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) Create an AOP proxy for the given bean.- 参数:
beanClass- the class of the beanbeanName- the name of the beanspecificInterceptors- 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- 返回:
- the AOP proxy for the bean
- 另请参阅:
-
createProxyClass
private Class<?> createProxyClass(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) -
buildProxy
private Object buildProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource, boolean classOnly) -
shouldSkip
Subclasses 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.- 参数:
beanClass- the class of the beanbeanName- the name of the bean- 返回:
- whether to skip the given bean
- 另请参阅:
-
AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX
-
isInfrastructureClass
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.
- 参数:
beanClass- the class of the bean- 返回:
- whether the bean represents an infrastructure class
- 另请参阅:
-
shouldProxyTargetClass
Determine whether the given bean should be proxied with its target class rather than its interfaces.Checks the
"preserveTargetClass" attributeof the corresponding bean definition.- 参数:
beanClass- the class of the beanbeanName- the name of the bean- 返回:
- whether the given bean should be proxied with its target class
- 另请参阅:
-
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.- 返回:
- whether the Advisors are pre-filtered
- 另请参阅:
-
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.- 参数:
beanName- the name of the beanspecificInterceptors- the set of interceptors that is specific to this bean (may be empty, but not null)- 返回:
- the array of Advisors for the given bean
-
resolveInterceptorNames
Resolves the specified interceptor names to Advisor objects. -
customizeProxyFactory
Subclasses may choose to implement this: for example, to change the interfaces exposed.The default implementation is empty.
- 参数:
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 targetSource) Return whether the given bean is to be proxied, what additional advices (e.g. AOP Alliance interceptors) and advisors to apply.- 参数:
beanClass- the class of the bean to advisebeanName- the name of the beantargetSource- the TargetSource returned by thegetCustomTargetSource(java.lang.Class<?>, java.lang.String)method: may be ignored. Will benullif no custom target source is in use.- 返回:
- 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. - 抛出:
infra.beans.BeansException- in case of errors- 另请参阅:
-