类 ProxyFactoryBean
- 所有已实现的接口:
Advised,TargetClassAware,infra.beans.factory.Aware,infra.beans.factory.BeanClassLoaderAware,infra.beans.factory.BeanFactoryAware,infra.beans.factory.FactoryBean<Object>,Serializable
FactoryBean implementation that builds an
AOP proxy based on beans in BeanFactory.
MethodInterceptors and
Advisors are identified by a list of bean
names in the current bean factory, specified through the "interceptorNames" property.
The last entry in the list can be the name of a target bean or a
TargetSource; however, it is normally preferable
to use the "targetName"/"target"/"targetSource" properties instead.
Global interceptors and advisors can be added at the factory level. The specified
ones are expanded in an interceptor list where an "xxx*" entry is included in the
list, matching the given prefix with the bean names (e.g. "global*" would match
both "globalBean1" and "globalBean2", "*" all defined interceptors). The matching
interceptors get applied according to their returned order value, if they implement
the Ordered interface.
Creates a JDK proxy when proxy interfaces are given, and a CGLIB proxy for the actual target class if not. Note that the latter will only work if the target class does not have final methods, as a dynamic subclass will be created at runtime.
It's possible to cast a proxy obtained from this factory to Advised,
or to obtain the ProxyFactoryBean reference and programmatically manipulate it.
This won't work for existing prototype references, which are independent. However,
it will work for prototypes subsequently obtained from the factory. Changes to
interception will work immediately on singletons (including existing references).
However, to change interfaces or target it's necessary to obtain a new instance
from the factory. This means that singleton instances obtained from the factory
do not have the same object identity. However, they do have the same interceptors
and target, and changing any reference will change all objects.
- 从以下版本开始:
- 4.0
- 作者:
- Rod Johnson, Juergen Hoeller, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static classUsed in the interceptor chain where we need to replace a bean with a prototype on creating a proxy.从类继承的嵌套类/接口 infra.aop.framework.AdvisedSupport
AdvisedSupport.MethodCacheKey -
字段概要
字段修饰符和类型字段说明private AdvisorAdapterRegistryprivate booleanWhether the advisor chain has already been initialized.private booleanprivate infra.beans.factory.BeanFactoryprivate booleanprivate booleanstatic final StringThis suffix in a value in an interceptor list indicates to expand globals.private String[]private static final infra.logging.Loggerprivate ClassLoaderprivate static final longprivate booleanprivate ObjectIf this is a singleton, the cached singleton proxy instance.private String从类继承的字段 infra.aop.framework.AdvisedSupport
advisorKey, EMPTY_TARGET_SOURCE, proxyMetadataCache, targetSource从类继承的字段 infra.aop.framework.ProxyConfig
exposeProxy, opaque从接口继承的字段 infra.beans.factory.FactoryBean
OBJECT_TYPE_ATTRIBUTE -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private voidInvoked when advice chain is created.private voidaddGlobalAdvisors(infra.beans.factory.BeanFactory beanFactory, String prefix) Add all global interceptors and pointcuts.protected voidBlow away and recache singleton on an advice change.private voidCheck the interceptorNames list whether it contains a target name as final element.protected Class<?>createCompositeInterface(Class<?>[] interfaces) Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.Return an independent advisor chain.private TargetSourceReturn a TargetSource to use when creating a proxy.Return a proxy.Class<?>Return the type of the proxy.protected ObjectReturn the proxy object to expose.private ObjectReturn the singleton instance of this class's proxy object, lazily creating it if it hasn't been created already.private voidCreate the advisor (interceptor) chain.private booleanisNamedBeanAnAdvisorOrAdvice(String beanName) Look at bean factory metadata to work out whether this bean name, which concludes the interceptorNames list, is an Advisor or Advice, or may be a target.booleanprivate AdvisornamedBeanToAdvisor(Object next) Convert the following object sourced from calling getBean() on a name in the interceptorNames array to an Advisor or TargetSource.private ObjectCreate a new prototype instance of this class's created proxy object, backed by an independent AdvisedSupport configuration.private voidvoidsetAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) Specify the AdvisorAdapterRegistry to use.voidsetAutodetectInterfaces(boolean autodetectInterfaces) Set whether to autodetect proxy interfaces if none specified.voidsetBeanClassLoader(ClassLoader classLoader) voidsetBeanFactory(infra.beans.factory.BeanFactory beanFactory) voidsetFrozen(boolean frozen) Set whether this config should be frozen.voidsetInterceptorNames(String... interceptorNames) Set the list of Advice/Advisor bean names.voidsetProxyClassLoader(ClassLoader classLoader) Set the ClassLoader to generate the proxy class in.voidsetProxyInterfaces(Class<?>[] proxyInterfaces) Set the names of the interfaces we're proxying.voidsetSingleton(boolean singleton) Set the value of the singleton property.voidsetTargetName(String targetName) Set the name of the target bean.从类继承的方法 infra.aop.framework.ProxyCreatorSupport
addListener, createAopProxy, getAopProxyFactory, isActive, removeListener, setAopProxyFactory从类继承的方法 infra.aop.framework.AdvisedSupport
addAdvice, addAdvice, addAdvisor, addAdvisor, addAdvisorInternal, addAdvisors, addAdvisors, addInterface, adviceIncluded, copyConfigurationFrom, copyConfigurationFrom, countAdvicesOfType, getAdvisorCount, getAdvisors, getAdvisorsInternal, getConfigurationOnlyCopy, getInterceptorChainFactory, getInterceptors, getProxiedInterfaces, getTargetClass, getTargetSource, hasUserSuppliedInterfaces, indexOf, indexOf, isInterfaceProxied, isPreFiltered, reduceToAdvisorKey, removeAdvice, removeAdvisor, removeAdvisor, removeInterface, replaceAdvisor, setInterceptorChainFactory, setInterfaces, setPreFiltered, setTarget, setTargetClass, setTargetSource, toProxyConfigString, toString从类继承的方法 infra.aop.framework.ProxyConfig
copyFrom, isExposeProxy, isFrozen, isOpaque, isOptimize, isProxyTargetClass, setExposeProxy, setOpaque, setOptimize, setProxyTargetClass从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait从接口继承的方法 infra.aop.framework.Advised
isExposeProxy, isFrozen, isProxyTargetClass, setExposeProxy
-
字段详细资料
-
serialVersionUID
private static final long serialVersionUID- 另请参阅:
-
logger
private static final infra.logging.Logger logger -
GLOBAL_SUFFIX
This suffix in a value in an interceptor list indicates to expand globals.- 另请参阅:
-
interceptorNames
-
targetName
-
autodetectInterfaces
private boolean autodetectInterfaces -
singleton
private boolean singleton -
advisorAdapterRegistry
-
freezeProxy
private boolean freezeProxy -
proxyClassLoader
-
classLoaderConfigured
private transient boolean classLoaderConfigured -
beanFactory
@Nullable private transient infra.beans.factory.BeanFactory beanFactory -
advisorChainInitialized
private boolean advisorChainInitializedWhether the advisor chain has already been initialized. -
singletonInstance
If this is a singleton, the cached singleton proxy instance.
-
-
构造器详细资料
-
ProxyFactoryBean
public ProxyFactoryBean()
-
-
方法详细资料
-
setProxyInterfaces
Set the names of the interfaces we're proxying. If no interface is given, a CGLIB for the actual class will be created.This is essentially equivalent to the "setInterfaces" method, but mirrors TransactionProxyFactoryBean's "setProxyInterfaces".
-
setInterceptorNames
Set the list of Advice/Advisor bean names. This must always be set to use this factory bean in a bean factory.The referenced beans should be of type Interceptor, Advisor or Advice The last entry in the list can be the name of any bean in the factory. If it's neither an Advice nor an Advisor, a new SingletonTargetSource is added to wrap it. Such a target bean cannot be used if the "target" or "targetSource" or "targetName" property is set, in which case the "interceptorNames" array must contain only Advice/Advisor bean names.
NOTE: Specifying a target bean as final name in the "interceptorNames" list is deprecated and will be removed in a future version. Use the
"gtargetName"property instead. -
setTargetName
Set the name of the target bean. This is an alternative to specifying the target name at the end of the "interceptorNames" array.You can also specify a target object or a TargetSource object directly, via the "target"/"targetSource" property, respectively.
-
setAutodetectInterfaces
public void setAutodetectInterfaces(boolean autodetectInterfaces) Set whether to autodetect proxy interfaces if none specified.Default is "true". Turn this flag off to create a CGLIB proxy for the full target class if no interfaces specified.
-
setSingleton
public void setSingleton(boolean singleton) Set the value of the singleton property. Governs whether this factory should always return the same proxy instance (which implies the same target) or whether it should return a new prototype instance, which implies that the target and interceptors may be new instances also, if they are obtained from prototype bean definitions. This allows for fine control of independence/uniqueness in the object graph. -
setAdvisorAdapterRegistry
Specify the AdvisorAdapterRegistry to use. Default is the global AdvisorAdapterRegistry. -
setFrozen
public void setFrozen(boolean frozen) 从类复制的说明:ProxyConfigSet whether this config should be frozen.When a config is frozen, no advice changes can be made. This is useful for optimization, and useful when we don't want callers to be able to manipulate configuration after casting to Advised.
- 覆盖:
setFrozen在类中ProxyConfig
-
setProxyClassLoader
Set the ClassLoader to generate the proxy class in.Default is the bean ClassLoader, i.e. the ClassLoader used by the containing BeanFactory for loading all bean classes. This can be overridden here for specific proxies.
-
setBeanClassLoader
- 指定者:
setBeanClassLoader在接口中infra.beans.factory.BeanClassLoaderAware
-
setBeanFactory
public void setBeanFactory(infra.beans.factory.BeanFactory beanFactory) - 指定者:
setBeanFactory在接口中infra.beans.factory.BeanFactoryAware
-
getObject
Return a proxy. Invoked when clients obtain beans from this factory bean. Create an instance of the AOP proxy to be returned by this factory. The instance will be cached for a singleton, and create on each call togetObject()for a proxy.- 指定者:
getObject在接口中infra.beans.factory.FactoryBean<Object>- 返回:
- a fresh AOP proxy reflecting the current state of this factory
- 抛出:
infra.beans.BeansException
-
getObjectType
Return the type of the proxy. Will check the singleton instance if already created, else fall back to the proxy interface (in case of just a single one), the target bean type, or the TargetSource's target class.- 指定者:
getObjectType在接口中infra.beans.factory.FactoryBean<Object>- 另请参阅:
-
isSingleton
public boolean isSingleton()- 指定者:
isSingleton在接口中infra.beans.factory.FactoryBean<Object>
-
createCompositeInterface
Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.The default implementation builds a JDK proxy class for the given interfaces.
- 参数:
interfaces- the interfaces to merge- 返回:
- the merged interface as Class
- 另请参阅:
-
getSingletonInstance
Return the singleton instance of this class's proxy object, lazily creating it if it hasn't been created already.- 返回:
- the shared singleton proxy
-
newPrototypeInstance
Create a new prototype instance of this class's created proxy object, backed by an independent AdvisedSupport configuration.- 返回:
- a totally independent proxy, whose advice we may manipulate in isolation
-
getProxy
Return the proxy object to expose.The default implementation uses a
getProxycall with the factory's bean class loader. Can be overridden to specify a custom class loader.- 参数:
aopProxy- the prepared AopProxy instance to get the proxy from- 返回:
- the proxy object to expose
- 另请参阅:
-
checkInterceptorNames
private void checkInterceptorNames()Check the interceptorNames list whether it contains a target name as final element. If found, remove the final name from the list and set it as targetName. -
isNamedBeanAnAdvisorOrAdvice
Look at bean factory metadata to work out whether this bean name, which concludes the interceptorNames list, is an Advisor or Advice, or may be a target.- 参数:
beanName- bean name to check- 返回:
trueif it's an Advisor or Advice
-
initializeAdvisorChain
Create the advisor (interceptor) chain. Advisors that are sourced from a BeanFactory will be refreshed each time a new prototype instance is added. Interceptors added programmatically through the factory API are unaffected by such changes.- 抛出:
AopConfigExceptioninfra.beans.BeansException
-
freshAdvisorChain
Return an independent advisor chain. We need to do this every time a new prototype instance is returned, to return distinct instances of prototype Advisors and Advices. -
addGlobalAdvisors
Add all global interceptors and pointcuts. -
addAdvisorOnChainCreation
Invoked when advice chain is created.Add the given advice, advisor or object to the interceptor list. Because of these three possibilities, we can't type the signature more strongly.
- 参数:
next- advice, advisor or target object
-
freshTargetSource
Return a TargetSource to use when creating a proxy. If the target was not specified at the end of the interceptorNames list, the TargetSource will be this class's TargetSource member. Otherwise, we get the target bean and wrap it in a TargetSource if necessary. -
namedBeanToAdvisor
Convert the following object sourced from calling getBean() on a name in the interceptorNames array to an Advisor or TargetSource. -
adviceChanged
protected void adviceChanged()Blow away and recache singleton on an advice change. -
readObject
-