我想使用DefaultAdvisorAutoProxyCreator创建一个线程局部对象(带有拦截器)。我知道如何使用ProxyFactoryObject:
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net">
<object id="ConsoleLoggingBeforeAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor" singleton="false">
<property name="Advice">
<object type="Spring.Examples.AopQuickStart.ConsoleLoggingBeforeAdvice"/>
</property>
</object>
<object id="ServiceCommandTargetSource" type="Spring.Aop.Target.ThreadLocalTargetSource">
<property name="TargetObjectName" value="ServiceCommandTarget"/>
</object>
<object id="ServiceCommandTarget" type="Spring.Examples.AopQuickStart.ServiceCommand" singleton="false"/>
<object name="ServiceCommand" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="TargetSource" ref="ServiceCommandTargetSource"/>
<property name="InterceptorNames">
<list>
<value>ConsoleLoggingBeforeAdvisor</value>
</list>
</property>
</object>
</objects>
但是,我不知道如何使用DefaultAdvisorAopCreator获得相同的效果。这是我尝试过的(但没有奏效):
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net">
<object id="ConsoleLoggingBeforeAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor" singleton="false">
<property name="Advice">
<object type="Spring.Examples.AopQuickStart.ConsoleLoggingBeforeAdvice"/>
</property>
</object>
<object id="ServiceCommand" type="Spring.Examples.AopQuickStart.ServiceCommand" singleton="false"/>
<object type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator">
<property name="CustomTargetSourceCreators">
<list element-type="Spring.Aop.Framework.AutoProxy.ITargetSourceCreator">
<object id="ThreadLocalTargetSourceCreator" type="Spring.Examples.AopQuickStart.ThreadLocalTargetSourceCreator"/>
</list>
</property>
</object>
</objects>
ThreadLocalTargetSourceCreator是一个无条件返回ThreadLocalTargetSource实例的自定义类:
namespace Spring.Examples.AopQuickStart {
public class ThreadLocalTargetSourceCreator : AbstractPrototypeTargetSourceCreator {
protected override AbstractPrototypeTargetSource CreatePrototypeTargetSource(Type objectType, string name, IObjectFactory factory) {
return new ThreadLocalTargetSource();
}
}
}
因此,总而言之,当我使用第一个配置(使用ProxyFactoryObject)从Spring.NET请求ServiceCommand时,每个线程只获得一个对象实例(正确的行为)。但是,使用第二个配置(DefaultAdvisorAutoProxyCreator),我每次都会获得一个新实例(不正确的行为;期望每个线程有一个实例)。
有什么想法吗?
答案 0 :(得分:0)
未经测试,但尝试此(或类似的东西):
protected override AbstractPrototypeTargetSource CreatePrototypeTargetSource(Type objectType, string name, IObjectFactory factory) {
ThreadLocalTargetSource ts = new ThreadLocalTargetSource();
ts.TargetObjectName = name;
ts.ObjectFactory = factory;
return ts;
}
答案 1 :(得分:0)
好的,我发现为什么它没有按预期工作。看起来很傻,我正在从AbstractPrototypeTargetSourceCreator.GetTargetSource()创建并返回ThreadLocalTargetSource的新实例。当然,ThreadLocalTargetSource的每个新实例都不知道之前由其“堂兄弟”创建的任何现有目标实例,因此每次请求实例时它都会创建其目标的新实例。
分辨率非常简单。我刚刚更新了ITargetSourceCreator的实现,以确保它创建了ThreadLocalTargetSource的单个实例,并在每次调用AbstractPrototypeTargetSourceCreator.GetTargetSource()时返回该实例:
namespace Spring.Examples.AopQuickStart {
public class ThreadLocalTargetSourceCreator : AbstractPrototypeTargetSourceCreator {
private readonly ThreadLocalTargetSource _threadLocalTargetSource;
public ThreadLocalTargetSourceCreator() {
_threadLocalTargetSource = new ThreadLocalTargetSource();
}
protected override AbstractPrototypeTargetSource CreatePrototypeTargetSource(Type objectType, string name, IObjectFactory factory) {
return _threadLocalTargetSource;
}
}
}
使用此代码,它对我来说非常有效,并且我获得了代理对象的线程本地生命时间。