org.springframework.aop.target
Class ThreadLocalTargetSource

java.lang.Object
  extended by org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource
      extended by org.springframework.aop.target.AbstractPrototypeBasedTargetSource
          extended by org.springframework.aop.target.ThreadLocalTargetSource
All Implemented Interfaces:
java.io.Serializable, ThreadLocalTargetSourceStats, TargetClassAware, TargetSource, BeanFactoryAware, DisposableBean

public class ThreadLocalTargetSource
extends AbstractPrototypeBasedTargetSource
implements ThreadLocalTargetSourceStats, DisposableBean

Alternative to an object pool. This TargetSource uses a threading model in which every thread has its own copy of the target. There's no contention for targets. Target object creation is kept to a minimum on the running server.

Application code is written as to a normal pool; callers can't assume they will be dealing with the same instance in invocations in different threads. However, state can be relied on during the operations of a single thread: for example, if one caller makes repeated calls on the AOP proxy.

Cleanup of thread-bound objects is performed on BeanFactory destruction, calling their DisposableBean.destroy() method if available. Be aware that many thread-bound objects can be around until the application actually shuts down.

Author:
Rod Johnson, Juergen Hoeller, Rob Harrop
See Also:
ThreadLocalTargetSourceStats, DisposableBean.destroy(), Serialized Form

Field Summary
private  int hitCount
           
private  int invocationCount
           
private  java.lang.ThreadLocal<java.lang.Object> targetInThread
          ThreadLocal holding the target associated with the current thread.
private  java.util.Set<java.lang.Object> targetSet
          Set of managed targets, enabling us to keep track of the targets we've created.
 
Fields inherited from class org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource
logger
 
Constructor Summary
ThreadLocalTargetSource()
           
 
Method Summary
 void destroy()
          Dispose of targets if necessary; clear ThreadLocal.
 int getHitCount()
          Return the number of hits that were satisfied by a thread-bound object.
 int getInvocationCount()
          Return the number of client invocations.
 int getObjectCount()
          Return the number of thread-bound objects created.
 IntroductionAdvisor getStatsMixin()
          Return an introduction advisor mixin that allows the AOP proxy to be cast to ThreadLocalInvokerStats.
 java.lang.Object getTarget()
          Implementation of abstract getTarget() method.
 
Methods inherited from class org.springframework.aop.target.AbstractPrototypeBasedTargetSource
destroyPrototypeInstance, newPrototypeInstance, setBeanFactory, writeReplace
 
Methods inherited from class org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource
copyFrom, equals, getBeanFactory, getTargetBeanName, getTargetClass, hashCode, isStatic, releaseTarget, setTargetBeanName, setTargetClass, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

targetInThread

private final java.lang.ThreadLocal<java.lang.Object> targetInThread
ThreadLocal holding the target associated with the current thread. Unlike most ThreadLocals, which are static, this variable is meant to be per thread per instance of the ThreadLocalTargetSource class.


targetSet

private final java.util.Set<java.lang.Object> targetSet
Set of managed targets, enabling us to keep track of the targets we've created.


invocationCount

private int invocationCount

hitCount

private int hitCount
Constructor Detail

ThreadLocalTargetSource

public ThreadLocalTargetSource()
Method Detail

getTarget

public java.lang.Object getTarget()
                           throws BeansException
Implementation of abstract getTarget() method. We look for a target held in a ThreadLocal. If we don't find one, we create one and bind it to the thread. No synchronization is required.

Specified by:
getTarget in interface TargetSource
Returns:
the target object, which contains the joinpoint
Throws:
BeansException

destroy

public void destroy()
Dispose of targets if necessary; clear ThreadLocal.

Specified by:
destroy in interface DisposableBean
See Also:
AbstractPrototypeBasedTargetSource.destroyPrototypeInstance(java.lang.Object)

getInvocationCount

public int getInvocationCount()
Description copied from interface: ThreadLocalTargetSourceStats
Return the number of client invocations.

Specified by:
getInvocationCount in interface ThreadLocalTargetSourceStats

getHitCount

public int getHitCount()
Description copied from interface: ThreadLocalTargetSourceStats
Return the number of hits that were satisfied by a thread-bound object.

Specified by:
getHitCount in interface ThreadLocalTargetSourceStats

getObjectCount

public int getObjectCount()
Description copied from interface: ThreadLocalTargetSourceStats
Return the number of thread-bound objects created.

Specified by:
getObjectCount in interface ThreadLocalTargetSourceStats

getStatsMixin

public IntroductionAdvisor getStatsMixin()
Return an introduction advisor mixin that allows the AOP proxy to be cast to ThreadLocalInvokerStats.