org.springframework.aop.framework
Class ProxyFactoryBean

java.lang.Object
  extended byorg.springframework.aop.framework.ProxyConfig
      extended byorg.springframework.aop.framework.AdvisedSupport
          extended byorg.springframework.aop.framework.ProxyFactoryBean
All Implemented Interfaces:
Advised, AdvisedSupportListener, BeanFactoryAware, FactoryBean, java.io.Serializable

public class ProxyFactoryBean
extends AdvisedSupport
implements FactoryBean, BeanFactoryAware, AdvisedSupportListener

FactoryBean implementation for use to source AOP proxies from a Spring BeanFactory.

Interceptors and Advisors are identified by a list of bean names in the current bean factory. These beans should be of type Interceptor or Advisor. The last entry in the list can be the name of any bean in the factory. If it's neither an Interceptor nor an Advisor, a new SingletonTargetSource is added to wrap it. If it;s a TargetSource, it is used as this proxy factory's TargetSource. It's normally preferred to use the "targetSource" property to set the TargetSource. It is not possible to use both the targetSource property and an interceptor name: this is treated as a configuration error.

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. An interceptor name list may not conclude with a global "xxx*" pattern, as global interceptors cannot invoke targets.

Creates a J2SE proxy when proxy interfaces are given, 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.

Author:
Rod Johnson, Juergen Hoeller
See Also:
setInterceptorNames(java.lang.String[]), setProxyInterfaces(java.lang.String[]), MethodInterceptor, Advised, SingletonTargetSource, Serialized Form

Nested Class Summary
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
static java.lang.String GLOBAL_SUFFIX
          This suffix in a value in an interceptor list indicates to expand globals.
 
Fields inherited from class org.springframework.aop.framework.AdvisedSupport
advisorChainFactory, EMPTY_TARGET_SOURCE, targetSource
 
Fields inherited from class org.springframework.aop.framework.ProxyConfig
exposeProxy, logger
 
Constructor Summary
ProxyFactoryBean()
           
 
Method Summary
 void activated(AdvisedSupport advisedSupport)
          Invoked when first proxy is created
 void adviceChanged(AdvisedSupport advisedSupport)
          Blow away and recache singleton on an advice change.
 java.lang.Object getObject()
          Return a proxy.
 java.lang.Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
 boolean isSingleton()
          Is the bean managed by this factory a singleton or a prototype?
 void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry)
          Specify the AdvisorAdapterRegistry to use.
 void setBeanFactory(BeanFactory beanFactory)
          Callback that supplies the owning factory to a bean instance.
 void setInterceptorNames(java.lang.String[] interceptorNames)
          Set the list of Advice/Advisor bean names.
 void setProxyInterfaces(java.lang.String[] interfaceNames)
          Set the names of the interfaces we're proxying.
 void setSingleton(boolean singleton)
          Set the value of the singleton property.
 
Methods inherited from class org.springframework.aop.framework.AdvisedSupport
addAdvice, addAdvice, addAdvisor, addAdvisor, addAdvisor, addAfterReturningAdvice, addBeforeAdvice, addInterceptor, addInterceptor, addInterface, addListener, addThrowsAdvice, adviceIncluded, copyConfigurationFrom, copyConfigurationFrom, countAdvicesOfType, countInterceptorsOfType, createAopProxy, getAdvisorChainFactory, getAdvisors, getProxiedInterfaces, getTargetSource, indexOf, indexOf, indexOf, interceptorIncluded, isActive, isInterfaceProxied, readResolve, removeAdvice, removeAdvisor, removeAdvisor, removeInterceptor, removeInterface, removeListener, replaceAdvisor, setAdvisorChainFactory, setInterfaces, setTarget, setTargetSource, toProxyConfigString, toString, writeReplace
 
Methods inherited from class org.springframework.aop.framework.ProxyConfig
copyFrom, getAopProxyFactory, getExposeProxy, getOpaque, getOptimize, getProxyTargetClass, isFrozen, setAopProxyFactory, setExposeProxy, setFrozen, setOpaque, setOptimize, setProxyTargetClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.aop.framework.Advised
getExposeProxy, getProxyTargetClass, isFrozen
 

Field Detail

GLOBAL_SUFFIX

public static final java.lang.String GLOBAL_SUFFIX
This suffix in a value in an interceptor list indicates to expand globals.

See Also:
Constant Field Values
Constructor Detail

ProxyFactoryBean

public ProxyFactoryBean()
Method Detail

setProxyInterfaces

public void setProxyInterfaces(java.lang.String[] interfaceNames)
                        throws java.lang.ClassNotFoundException
Set the names of the interfaces we're proxying. If no interface is given, a CGLIB for the actual class will be created.

Throws:
java.lang.ClassNotFoundException

setInterceptorNames

public void setInterceptorNames(java.lang.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 property is set, in which case the interceptorNames array must contain only Advice/Advisor bean names.

See Also:
MethodInterceptor, Advisor, Advice, SingletonTargetSource

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.

Parameters:
singleton -

setAdvisorAdapterRegistry

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

See Also:
GlobalAdvisorAdapterRegistry

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
                    throws BeansException
Description copied from interface: BeanFactoryAware
Callback that supplies the owning factory to a bean instance.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (may not be null). The bean can immediately call methods on the factory.
Throws:
BeansException - in case of initialization errors
See Also:
BeanInitializationException

getObject

public java.lang.Object getObject()
                           throws BeansException
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.

Specified by:
getObject in interface FactoryBean
Returns:
Object a fresh AOP proxy reflecting the current state of this factory
Throws:
BeansException

getObjectType

public java.lang.Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance. This allows to check for specific types of beans without instantiating objects, e.g. on autowiring.

For a singleton, this can simply return getObject().getClass(), or even null, as autowiring will always check the actual objects for singletons. For prototypes, returning a meaningful type here is highly advisable, as autowiring will simply ignore them else.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class, boolean, boolean)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the bean managed by this factory a singleton or a prototype? That is, will getObject() always return the same object?

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory.

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton
See Also:
FactoryBean.isSingleton()

activated

public void activated(AdvisedSupport advisedSupport)
Description copied from interface: AdvisedSupportListener
Invoked when first proxy is created

Specified by:
activated in interface AdvisedSupportListener
Parameters:
advisedSupport -
See Also:
AdvisedSupportListener.activated(org.springframework.aop.framework.AdvisedSupport)

adviceChanged

public void adviceChanged(AdvisedSupport advisedSupport)
Blow away and recache singleton on an advice change.

Specified by:
adviceChanged in interface AdvisedSupportListener
Parameters:
advisedSupport -


Copyright (C) 2003-2004 The Spring Framework Project.