如何在Spring.NET中使用DefaultAdvisorAutoProxyCreator定义线程范围的对象 [英] How to Define Thread-scoped objects with DefaultAdvisorAutoProxyCreator in Spring.NET

查看:85
本文介绍了如何在Spring.NET中使用DefaultAdvisorAutoProxyCreator定义线程范围的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用DefaultAdvisorAutoProxyCreator创建一个线程局部对象(带有拦截器).我知道如何使用ProxyFactoryObject做到这一点:

I want to create a thread-local object (with interceptors) using DefaultAdvisorAutoProxyCreator. I know how to do that using 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获得相同的效果.这是我尝试过的方法(但是没有用):

However, I don't know how to get the same effect using DefaultAdvisorAopCreator. Here's what I tried (but didn't work):

<?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实例:

ThreadLocalTargetSourceCreator is a custom class that unconditionally returns a ThreadLocalTargetSource instance:

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),每次都会得到一个新实例(行为不正确;每个线程需要一个实例).

So, in summary, when I request ServiceCommand from Spring.NET with the first config (using ProxyFactoryObject), I get only one instance of the object per thread (correct behavior). However, with the second config (DefaultAdvisorAutoProxyCreator), I get a new instance every time (incorrect behavior; expecting one instance per thread).

有什么想法吗?

推荐答案

好,我发现了为什么它没有按预期工作.看起来很傻,我正在从AbstractPrototypeTargetSourceSourceCreator.GetTargetSource()创建并返回ThreadLocalTargetSource的新实例.当然,每个ThreadLocalTargetSource的新实例以前都不知道其表兄弟"创建的任何现有目标实例,因此,每次请求一个实例时,它都会为其目标创建一个新实例.

Ok, I found out why it was not working as expected. Silly as it seems, I was creating and returning a new instance of ThreadLocalTargetSource from AbstractPrototypeTargetSourceCreator.GetTargetSource(). Of course, each new instance of ThreadLocalTargetSource would have no clue of any existing target instances created by its "cousins" before, so it would create a new instance of its target every time an instance was requested.

解决方法非常简单.我刚刚更新了ITargetSourceCreator的实现,以确保它创建了ThreadLocalTargetSource的单个实例,并在每次调用AbstractPrototypeTargetSourceCreator.GetTargetSource()时都返回了该实例:

The resolution was very simple. I just updated my implementation of ITargetSourceCreator to make sure it created a single instance of ThreadLocalTargetSource and returned that instance each time AbstractPrototypeTargetSourceCreator.GetTargetSource() was called:

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;
        }
    }
}

使用此代码,它对我来说非常理想,并且我可以为代理对象获得线程本地的生存时间.

With this code, it works perfectly for me and I get a thread-local life time for my proxied objects.

这篇关于如何在Spring.NET中使用DefaultAdvisorAutoProxyCreator定义线程范围的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆