org.springframework.transaction.interceptor
Class TransactionProxyFactoryBean

java.lang.Object
  extended by org.springframework.aop.framework.ProxyConfig
      extended by org.springframework.transaction.interceptor.TransactionProxyFactoryBean
All Implemented Interfaces:
Serializable, BeanFactoryAware, FactoryBean, InitializingBean

public class TransactionProxyFactoryBean
extends ProxyConfig
implements FactoryBean, BeanFactoryAware, InitializingBean

Proxy factory bean for simplified declarative transaction handling. Alternative to a standard AOP ProxyFactoryBean with a separate TransactionInterceptor definition.

This class is intended to cover the typical case of declarative transaction demarcation: namely, wrapping a singleton target object with a transactional proxy, proxying all the interfaces that the target implements.

There are three main properties to be specified:

If the "transactionManager" property is not set explicitly and this FactoryBean is running in a ListableBeanFactory, a single matching bean of type PlatformTransactionManager will be fetched from the BeanFactory.

In contrast to TransactionInterceptor, the transaction attributes are specified as properties, with method names as keys and transaction attribute descriptors as values. Method names are always applied to the target class.

Internally, a TransactionInterceptor instance is used, but the user of this class does not have to care. Optionally, a MethodPointcut can be specified to cause conditional invocation of the underlying TransactionInterceptor.

The "preInterceptors" and "postInterceptors" properties can be set to add additional interceptors to the mix, like PerformanceMonitorInterceptor or HibernateInterceptor/JdoInterceptor.

HINT: This class is often used with parent/child bean definitions. Typically, you will define the transaction manager and default transaction attributes (for method name patterns) in an abstract parent bean definition, deriving concrete child bean definitions for specific target objects. This reduces the per-bean definition effort to a minimum.

 <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
     abstract="true">
   <property name="transactionManager" ref="transactionManager"/>
   <property name="transactionAttributes">
     <props>
       <prop key="insert*">PROPAGATION_REQUIRED</prop>
       <prop key="update*">PROPAGATION_REQUIRED</prop>
       <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
     </props>
   </property>
 </bean>

 <bean id="myProxy" parent="baseTransactionProxy">
   <property name="target" ref="myTarget"/>
 </bean>

 <bean id="yourProxy" parent="baseTransactionProxy">
   <property name="target" ref="yourTarget"/>
 </bean>

Since:
21.08.2003
Author:
Juergen Hoeller, Dmitriy Kopylenko, Rod Johnson
See Also:
setTransactionManager(org.springframework.transaction.PlatformTransactionManager), setTarget(java.lang.Object), setTransactionAttributes(java.util.Properties), TransactionInterceptor, ProxyFactoryBean, Serialized Form

Field Summary
 
Fields inherited from class org.springframework.aop.framework.ProxyConfig
exposeProxy, logger
 
Constructor Summary
TransactionProxyFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected  TargetSource createTargetSource(Object target)
          Set the target or TargetSource.
 Object getObject()
          Return an instance (possibly shared or independent) of the object managed by this factory.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
protected  Object getProxy(AopProxy aopProxy)
          Return the proxy object to expose.
 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)
          This callback is optional: If running in a BeanFactory and no transaction manager has been set explicitly, a single matching bean of type PlatformTransactionManager will be fetched from the BeanFactory.
 void setPointcut(Pointcut pointcut)
          Set a pointcut, i.e a bean that can cause conditional invocation of the TransactionInterceptor depending on method and attributes passed.
 void setPostInterceptors(Object[] postInterceptors)
          Set additional interceptors (or advisors) to be applied after the implicit transaction interceptor, e.g.
 void setPreInterceptors(Object[] preInterceptors)
          Set additional interceptors (or advisors) to be applied before the implicit transaction interceptor, e.g.
 void setProxyInterfaces(String[] interfaceNames)
          Specify the set of interfaces being proxied.
 void setTarget(Object target)
          Set the target object, i.e. the bean to be wrapped with a transactional proxy.
 void setTransactionAttributes(Properties transactionAttributes)
          Set properties with method names as keys and transaction attribute descriptors (parsed via TransactionAttributeEditor) as values: e.g. key = "myMethod", value = "PROPAGATION_REQUIRED,readOnly".
 void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource)
          Set the transaction attribute source which is used to find transaction attributes.
 void setTransactionManager(PlatformTransactionManager transactionManager)
          Set the transaction manager.
 
Methods inherited from class org.springframework.aop.framework.ProxyConfig
copyFrom, getAopProxyFactory, isExposeProxy, isFrozen, isOpaque, isOptimize, isProxyTargetClass, setAopProxyFactory, setExposeProxy, setFrozen, setOpaque, setOptimize, setProxyTargetClass, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TransactionProxyFactoryBean

public TransactionProxyFactoryBean()
Method Detail

setTransactionManager

public void setTransactionManager(PlatformTransactionManager transactionManager)
Set the transaction manager. This will perform actual transaction management: This class is just a way of invoking it.

See Also:
TransactionAspectSupport.setTransactionManager(org.springframework.transaction.PlatformTransactionManager)

setTarget

public void setTarget(Object target)
Set the target object, i.e. the bean to be wrapped with a transactional proxy.

The target may be any object, in which case a SingletonTargetSource will be created. If it is a TargetSource, no wrapper TargetSource is created: This enables the use of a pooling or prototype TargetSource etc.

See Also:
TargetSource, SingletonTargetSource, LazyInitTargetSource, PrototypeTargetSource, CommonsPoolTargetSource

setProxyInterfaces

public void setProxyInterfaces(String[] interfaceNames)
                        throws ClassNotFoundException
Specify the set of interfaces being proxied.

If left null (the default), the AOP infrastructure works out which interfaces need proxying by analyzing the target, proxying all the interfaces that the target object implements.

Throws:
ClassNotFoundException

setTransactionAttributes

public void setTransactionAttributes(Properties transactionAttributes)
Set properties with method names as keys and transaction attribute descriptors (parsed via TransactionAttributeEditor) as values: e.g. key = "myMethod", value = "PROPAGATION_REQUIRED,readOnly".

Note: Method names are always applied to the target class, no matter if defined in an interface or the class itself.

Internally, a NameMatchTransactionAttributeSource will be created from the given properties.

See Also:
setTransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource), TransactionAspectSupport.setTransactionAttributes(java.util.Properties), TransactionAttributeEditor, NameMatchTransactionAttributeSource

setTransactionAttributeSource

public void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource)
Set the transaction attribute source which is used to find transaction attributes. If specifying a String property value, a PropertyEditor will create a MethodMapTransactionAttributeSource from the value.

See Also:
setTransactionAttributes(java.util.Properties), TransactionAspectSupport.setTransactionAttributeSource(org.springframework.transaction.interceptor.TransactionAttributeSource), TransactionAttributeSourceEditor, MethodMapTransactionAttributeSource, NameMatchTransactionAttributeSource, AttributesTransactionAttributeSource, AnnotationTransactionAttributeSource

setPointcut

public void setPointcut(Pointcut pointcut)
Set a pointcut, i.e a bean that can cause conditional invocation of the TransactionInterceptor depending on method and attributes passed. Note: Additional interceptors are always invoked.

See Also:
setPreInterceptors(java.lang.Object[]), setPostInterceptors(java.lang.Object[])

setPreInterceptors

public void setPreInterceptors(Object[] preInterceptors)
Set additional interceptors (or advisors) to be applied before the implicit transaction interceptor, e.g. PerformanceMonitorInterceptor.

See Also:
PerformanceMonitorInterceptor

setPostInterceptors

public void setPostInterceptors(Object[] postInterceptors)
Set additional interceptors (or advisors) to be applied after the implicit transaction interceptor, e.g. HibernateInterceptors for eagerly binding Sessions to the current thread when using JTA.

Note that this is just necessary if you rely on those interceptors in general: HibernateTemplate and JdoTemplate work nicely with JtaTransactionManager through implicit on-demand thread binding.

See Also:
HibernateInterceptor, JdoInterceptor

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)
This callback is optional: If running in a BeanFactory and no transaction manager has been set explicitly, a single matching bean of type PlatformTransactionManager will be fetched from the BeanFactory.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (may not be null). The bean can immediately call methods on the factory.
See Also:
BeanFactoryUtils.beanOfTypeIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory, java.lang.Class), PlatformTransactionManager

afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean

createTargetSource

protected TargetSource createTargetSource(Object target)
Set the target or TargetSource.

Parameters:
target - target. If this is an implementation of TargetSource it is used as our TargetSource; otherwise it is wrapped in a SingletonTargetSource.
Returns:
a TargetSource for this object

getProxy

protected Object getProxy(AopProxy aopProxy)
Return the proxy object to expose.

Default implementation uses a plain getProxy() call. Can be overridden to specify a custom class loader.

Parameters:
aopProxy - the prepared AopProxy instance to get the proxy from
Returns:
the proxy object to expose
See Also:
AopProxy.getProxy(), AopProxy.getProxy(ClassLoader)

getObject

public Object getObject()
Description copied from interface: FactoryBean
Return an instance (possibly shared or independent) of the object managed by this factory. As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

If this method returns null, the factory will consider the FactoryBean as not fully initialized and throw a corresponding FactoryBeanNotInitializedException.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (should not be null; a null value will be considered as an indication of incomplete initialization)
See Also:
FactoryBeanNotInitializedException

getObjectType

public 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, for example on autowiring.

For a singleton, this should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

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

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

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


Copyright (c) 2002-2006 The Spring Framework Project.