org.springframework.aop.target
Class ThreadLocalTargetSource

org.springframework.aop.target.AbstractPrototypeTargetSource
  |
  +--org.springframework.aop.target.ThreadLocalTargetSource

public final class ThreadLocalTargetSource
extends AbstractPrototypeTargetSource
implements DisposableBean, ThreadLocalTargetSourceStats

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 is performed in the destroy() method from DisposableBean.

Author:
Rod Johnson
Version: $Id: ThreadLocalTargetSource.java,v 1.6 2004/01/21 20:21:35 johnsonr Exp $

Method Summary
 voiddestroy()
          Dispose of targets if necessary; clear ThreadLocal.
 intgetHits()
           
 intgetInvocations()
           
 intgetObjects()
           
 InterceptionIntroductionAdvisorgetStatsMixin()
          Return an introduction advisor mixin that allows the AOP proxy to be case to ThreadLocalInvokerStats.
 ObjectgetTarget()
          Implementation of abstract getTarget() method.
 voidreleaseTarget(Object o)
           

Methods inherited from class org.springframework.aop.target.AbstractPrototypeTargetSource
afterPropertiesSet, getTargetBeanName, getTargetClass, isStatic, setBeanFactory, setTargetBeanName

Method Detail

destroy

public void destroy()
Dispose of targets if necessary; clear ThreadLocal.
See Also:
org.springframework.beans.factory.DisposableBean.destroy()

getHits

public int getHits()

getInvocations

public int getInvocations()

getObjects

public int getObjects()

getStatsMixin

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

getTarget

public Object getTarget()
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.

releaseTarget

public void releaseTarget(Object o)
See Also:
org.springframework.aop.TargetSource.releaseTarget(java.lang.Object)

Association Links

to Class java.lang.ThreadLocal

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.

to Class java.util.Set

Set of managed targets, enabling us to keep track of the targets we've created.