1

我想使用 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),我每次都会得到一个新实例(不正确的行为;期望每个线程一个实例)。

有什么想法吗?

4

2 回答 2

0

未经测试,但试试这个(或类似的东西):

protected override AbstractPrototypeTargetSource CreatePrototypeTargetSource(Type objectType, string name, IObjectFactory factory) {
  ThreadLocalTargetSource ts = new ThreadLocalTargetSource();
  ts.TargetObjectName = name;
  ts.ObjectFactory = factory;
  return ts;
}
于 2012-02-15T07:43:40.127 回答
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;
        }
    }
}

使用此代码,它非常适合我,并且我的代理对象获得了线程本地生命周期。

于 2012-02-17T03:16:08.500 回答