Top Description Inners Fields Constructors Methods
org.springframework.aop.framework

public Class ProxyFactoryBean

extends ProxyCreatorSupport
implements FactoryBean<Object>, BeanClassLoaderAware, BeanFactoryAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.FactoryBean
Annotations
@SuppressWarnings:serial
Imports
java.io.IOException, .ObjectInputStream, .Serializable, java.util.ArrayList, .Arrays, .List, org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.apache.commons.logging.Log, .LogFactory, org.springframework.aop.Advisor, .TargetSource, org.springframework.aop.framework.adapter.AdvisorAdapterRegistry, .GlobalAdvisorAdapterRegistry, .UnknownAdviceTypeException, org.springframework.aop.target.SingletonTargetSource, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanClassLoaderAware, .BeanFactory, .BeanFactoryAware, .BeanFactoryUtils, .FactoryBean, .FactoryBeanNotInitializedException, .ListableBeanFactory, org.springframework.core.annotation.AnnotationAwareOrderComparator, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ObjectUtils

org.springframework.beans.factory.FactoryBean implementation that builds an AOP proxy based on beans in a Spring org.springframework.beans.factory.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 org.springframework.aop.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 — for example, "global*" would match both "globalBean1" and "globalBean2"; whereas, "*" would match all defined interceptors. The matching interceptors get applied according to their returned order value, if they implement the org.springframework.core.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 a 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.

Authors
Rod Johnson, Juergen Hoeller
See Also
setInterceptorNames, setProxyInterfaces, org.aopalliance.intercept.MethodInterceptor, org.springframework.aop.Advisor, Advised

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ProxyFactoryBean.PrototypePlaceholderAdvisor

Used in the interceptor chain where we need to replace a bean with a prototype on creating a proxy.

Field Summary

Modifier and TypeField and Description
private AdvisorAdapterRegistry
private boolean
advisorChainInitialized

Whether the advisor chain has already been initialized.

private boolean
private transient BeanFactory
private transient boolean
private boolean
public static final String
GLOBAL_SUFFIX

This suffix in a value in an interceptor list indicates to expand globals.

private String[]
protected final Log
private transient ClassLoader
private boolean
private Object
singletonInstance

If this is a singleton, the cached singleton proxy instance.

private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
addAdvisorOnChainCreation(Object
advice, advisor or target object
next
)

Invoked when advice chain is created.

private void
addGlobalAdvisors(ListableBeanFactory beanFactory, String prefix)

Add all global interceptors and pointcuts.

protected void
adviceChanged()

Overrides org.springframework.aop.framework.ProxyCreatorSupport.adviceChanged.

Blow away and recache singleton on an advice change.
private void
checkInterceptorNames()

Check the interceptorNames list whether it contains a target name as final element.

private List<Advisor>
freshAdvisorChain()

Return an independent advisor chain.

private TargetSource
freshTargetSource()

Return a TargetSource to use when creating a proxy.

public Object

Returns:

a fresh AOP proxy reflecting the current state of this factory
getObject
()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Return a proxy.
public Class<?>
protected Object

Returns:

the proxy object to expose
getProxy
(AopProxy
the prepared AopProxy instance to get the proxy from
aopProxy
)

Return the proxy object to expose.

private synchronized Object

Returns:

the shared singleton proxy
getSingletonInstance
()

Return the singleton instance of this class's proxy object, lazily creating it if it hasn't been created already.

private synchronized void
initializeAdvisorChain()

Create the advisor (interceptor) chain.

private boolean

Returns:

true if it's an Advisor or Advice
isNamedBeanAnAdvisorOrAdvice
(String
bean name to check
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.

public boolean
isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton?
private Advisor
namedBeanToAdvisor(Object next)

Convert the following object sourced from calling getBean() on a name in the interceptorNames array to an Advisor or TargetSource.

private synchronized Object

Returns:

a totally independent proxy, whose advice we may manipulate in isolation
newPrototypeInstance
()

Create a new prototype instance of this class's created proxy object, backed by an independent AdvisedSupport configuration.

private void
public void
setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)

Specify the AdvisorAdapterRegistry to use.

public void
setAutodetectInterfaces(boolean autodetectInterfaces)

Set whether to autodetect proxy interfaces if none specified.

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.
public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setFrozen(boolean frozen)

Overrides org.springframework.aop.framework.ProxyConfig.setFrozen.

Set whether this config should be frozen.
public void
setInterceptorNames(String... interceptorNames)

Set the list of Advice/Advisor bean names.

public void
setProxyClassLoader(ClassLoader classLoader)

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.

public void
setProxyInterfaces(Class<?>[] proxyInterfaces)

Set the names of the interfaces we're proxying.

public void
setSingleton(boolean singleton)

Set the value of the singleton property.

public void
setTargetName(String targetName)

Set the name of the target bean.

Inherited from org.springframework.aop.framework.ProxyCreatorSupport:
addListenercreateAopProxygetAopProxyFactoryisActiveremoveListenersetAopProxyFactory

Field Detail

advisorAdapterRegistryback to summary
private AdvisorAdapterRegistry advisorAdapterRegistry
advisorChainInitializedback to summary
private boolean advisorChainInitialized

Whether the advisor chain has already been initialized.

autodetectInterfacesback to summary
private boolean autodetectInterfaces
beanFactoryback to summary
private transient BeanFactory beanFactory
Annotations
@Nullable
classLoaderConfiguredback to summary
private transient boolean classLoaderConfigured
freezeProxyback to summary
private boolean freezeProxy
GLOBAL_SUFFIXback to summary
public static final String GLOBAL_SUFFIX

This suffix in a value in an interceptor list indicates to expand globals.

interceptorNamesback to summary
private String[] interceptorNames
Annotations
@Nullable
loggerback to summary
protected final Log logger
proxyClassLoaderback to summary
private transient ClassLoader proxyClassLoader
Annotations
@Nullable
singletonback to summary
private boolean singleton
singletonInstanceback to summary
private Object singletonInstance

If this is a singleton, the cached singleton proxy instance.

Annotations
@Nullable
targetNameback to summary
private String targetName
Annotations
@Nullable

Constructor Detail

ProxyFactoryBeanback to summary
public ProxyFactoryBean()

Method Detail

addAdvisorOnChainCreationback to summary
private void addAdvisorOnChainCreation(Object next)

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.

Parameters
next:Object

advice, advisor or target object

addGlobalAdvisorsback to summary
private void addGlobalAdvisors(ListableBeanFactory beanFactory, String prefix)

Add all global interceptors and pointcuts.

adviceChangedback to summary
protected void adviceChanged()

Overrides org.springframework.aop.framework.ProxyCreatorSupport.adviceChanged.

Blow away and recache singleton on an advice change.

Annotations
@Override
checkInterceptorNamesback to summary
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.

freshAdvisorChainback to summary
private List<Advisor> 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.

freshTargetSourceback to summary
private TargetSource 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.

getObjectback to summary
public Object getObject() throws BeansException

Implements org.springframework.beans.factory.FactoryBean.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 to getObject() for a proxy.

Returns:Object

a fresh AOP proxy reflecting the current state of this factory

Annotations
@Override
@Nullable
getObjectTypeback to summary
public Class<?> getObjectType()

Implements org.springframework.beans.factory.FactoryBean.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.

Returns:Class<?>

Doc from org.springframework.beans.factory.FactoryBean.getObjectType.

the type of object that this FactoryBean creates, or null if not known at the time of the call

Annotations
@Override
@Nullable
See Also
org.springframework.aop.framework.AopProxy#getProxyClass
getProxyback to summary
protected Object getProxy(AopProxy aopProxy)

Return the proxy object to expose.

The default implementation uses a getProxy call with the factory's bean class loader. Can be overridden to specify a custom class loader.

Parameters
aopProxy:AopProxy

the prepared AopProxy instance to get the proxy from

Returns:Object

the proxy object to expose

See Also
AopProxy#getProxy(ClassLoader)
getSingletonInstanceback to summary
private synchronized Object getSingletonInstance()

Return the singleton instance of this class's proxy object, lazily creating it if it hasn't been created already.

Returns:Object

the shared singleton proxy

initializeAdvisorChainback to summary
private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException

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.

isNamedBeanAnAdvisorOrAdviceback to summary
private boolean isNamedBeanAnAdvisorOrAdvice(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.

Parameters
beanName:String

bean name to check

Returns:boolean

true if it's an Advisor or Advice

isSingletonback to summary
public boolean isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Doc from org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton? That is, will getObject() always return the same object (a reference that can be cached)?

Note

If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

Note

This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean#isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

The default implementation returns true, since a FactoryBean typically manages a singleton instance.

Returns:boolean

whether the exposed object is a singleton

Annotations
@Override
namedBeanToAdvisorback to summary
private Advisor namedBeanToAdvisor(Object next)

Convert the following object sourced from calling getBean() on a name in the interceptorNames array to an Advisor or TargetSource.

newPrototypeInstanceback to summary
private synchronized Object newPrototypeInstance()

Create a new prototype instance of this class's created proxy object, backed by an independent AdvisedSupport configuration.

Returns:Object

a totally independent proxy, whose advice we may manipulate in isolation

readObjectback to summary
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException

Hides org.springframework.aop.framework.AdvisedSupport.readObject.

setAdvisorAdapterRegistryback to summary
public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)

Specify the AdvisorAdapterRegistry to use. Default is the global AdvisorAdapterRegistry.

See Also
org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry
setAutodetectInterfacesback to summary
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.

See Also
setProxyTargetClass
setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's InitializingBean#afterPropertiesSet() method or a custom init-method.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

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

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
setFrozenback to summary
public void setFrozen(boolean frozen)

Overrides org.springframework.aop.framework.ProxyConfig.setFrozen.

Doc from org.springframework.aop.framework.ProxyConfig.setFrozen.

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

Annotations
@Override
setInterceptorNamesback to summary
public void setInterceptorNames(String... interceptorNames)

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 Spring version. Use the "targetName" property instead.

See Also
org.aopalliance.intercept.MethodInterceptor, org.springframework.aop.Advisor, org.aopalliance.aop.Advice, org.springframework.aop.target.SingletonTargetSource
setProxyClassLoaderback to summary
public void setProxyClassLoader(ClassLoader classLoader)

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.

setProxyInterfacesback to summary
public void setProxyInterfaces(Class<?>[] proxyInterfaces) throws ClassNotFoundException

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

See Also
setInterfaces, AbstractSingletonProxyFactoryBean#setProxyInterfaces
setSingletonback to summary
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.

setTargetNameback to summary
public void setTargetName(String targetName)

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.

See Also
setInterceptorNames(String[]), setTarget(Object), setTargetSource(org.springframework.aop.TargetSource)
org.springframework.aop.framework back to summary

private Class ProxyFactoryBean.PrototypePlaceholderAdvisor

extends Object
implements Advisor, Serializable
Class Inheritance
  • java.lang.Object
  • org.springframework.aop.framework.ProxyFactoryBean.PrototypePlaceholderAdvisor
All Implemented Interfaces
java.io.Serializable, org.springframework.aop.Advisor

Used in the interceptor chain where we need to replace a bean with a prototype on creating a proxy.

Field Summary

Modifier and TypeField and Description
private final String
private final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Advice
getAdvice()

Implements org.springframework.aop.Advisor.getAdvice.

Return the advice part of this aspect.
public String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

beanNameback to summary
private final String beanName
messageback to summary
private final String message

Constructor Detail

PrototypePlaceholderAdvisorback to summary
public PrototypePlaceholderAdvisor(String beanName)

Method Detail

getAdviceback to summary
public Advice getAdvice()

Implements org.springframework.aop.Advisor.getAdvice.

Doc from org.springframework.aop.Advisor.getAdvice.

Return the advice part of this aspect. An advice may be an interceptor, a before advice, a throws advice, etc.

Returns:Advice

the advice that should apply if the pointcut matches

Annotations
@Override
getBeanNameback to summary
public String getBeanName()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override