org.springframework.aop.target
public class: ThreadLocalTargetSource [javadoc |
source]
java.lang.Object
org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource
org.springframework.aop.target.AbstractPrototypeBasedTargetSource
org.springframework.aop.target.ThreadLocalTargetSource
All Implemented Interfaces:
DisposableBean, ThreadLocalTargetSourceStats, TargetSource, BeanFactoryAware, Serializable
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.
Also see:
- ThreadLocalTargetSourceStats
- org.springframework.beans.factory.DisposableBean#destroy()
- author:
Rod - Johnson
- author:
Juergen - Hoeller
- author:
Rob - Harrop
| Methods from org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource: |
|---|
|
copyFrom, equals, getBeanFactory, getTargetBeanName, getTargetClass, hashCode, isStatic, releaseTarget, setBeanFactory, setTargetBeanName, setTargetClass, toString, writeReplace |
| Method from org.springframework.aop.target.ThreadLocalTargetSource Detail: |
public void destroy() {
logger.debug("Destroying ThreadLocalTargetSource bindings");
synchronized (this.targetSet) {
for (Iterator it = this.targetSet.iterator(); it.hasNext(); ) {
destroyPrototypeInstance(it.next());
}
this.targetSet.clear();
}
// Clear ThreadLocal, just in case.
this.targetInThread.set(null);
}
Dispose of targets if necessary; clear ThreadLocal. |
public int getHitCount() {
return this.hitCount;
}
|
public int getInvocationCount() {
return this.invocationCount;
}
|
public int getObjectCount() {
return this.targetSet.size();
}
|
public IntroductionAdvisor getStatsMixin() {
DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this);
return new DefaultIntroductionAdvisor(dii, ThreadLocalTargetSourceStats.class);
}
Return an introduction advisor mixin that allows the AOP proxy to be
cast to ThreadLocalInvokerStats. |
public Object getTarget() throws BeansException {
++this.invocationCount;
Object target = this.targetInThread.get();
if (target == null) {
if (logger.isDebugEnabled()) {
logger.debug("No target for prototype '" + getTargetBeanName() + "' bound to thread: " +
"creating one and binding it to thread '" + Thread.currentThread().getName() + "'");
}
// Associate target with ThreadLocal.
target = newPrototypeInstance();
this.targetInThread.set(target);
this.targetSet.add(target);
}
else {
++this.hitCount;
}
return target;
}
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. |