我是新來Spring.NET和我只是玩弄嘗試不同的東西出來與DefaultAdvisorAutoProxyCreator的定義原型攔截。由於我測試的一部分,我創建了一個簡單的對象:如何在Spring.NET
public interface ICommand {
void Execute(object context);
}
一個實現的:
public class ServiceCommand : ICommand {
public ServiceCommand() {
Console.WriteLine("########## {0} ##########", GetType().Name);
}
public void Execute(object context) {
Console.WriteLine("Service implementation: {0}.{1}", GetType().Name, MethodBase.GetCurrentMethod().Name);
}
}
最後,我建議之前已經簡單如下:
public class ConsoleLoggingBeforeAdvice : IMethodBeforeAdvice {
public ConsoleLoggingBeforeAdvice() {
Console.WriteLine("########## {0} ##########", GetType().Name);
}
public void Before(MethodInfo method, object[] args, object target) {
Console.WriteLine("Intercepted call to this method: {0}", method.Name);
Console.WriteLine(" The target is : {0}", target);
Console.WriteLine(" The arguments are : ");
if (args != null) {
foreach (object arg in args) {
Console.WriteLine("\t: {0}", arg);
}
}
}
}
由於你可以看到,這些東西大部分來自Spring.NET快速入門示例。
所以,我構造成包裹在通過一個ProxyFactoryObject的ConsoleLoggingBeforeAdvice和ServiceCommand標記兩個對象爲原型(見下面的配置)。這按預期工作:每次我們要求ServiceCommand,無論是對象的新實例和相關的攔截時間創建:
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net">
<object id="ConsoleLoggingBeforeAdvice" type="Spring.Aop.Support.DefaultPointcutAdvisor" singleton="false">
<property name="Advice">
<object type="Spring.Examples.AopQuickStart.ConsoleLoggingBeforeAdvice"/>
</property>
</object>
<object id="ServiceCommandTarget" type="Spring.Examples.AopQuickStart.ServiceCommand" singleton="false"/>
<object id="ServiceCommand" type ="Spring.Aop.Framework.ProxyFactoryObject">
<property name="IsSingleton" value="false"/>
<property name="TargetName" value="ServiceCommandTarget"/>
<property name="InterceptorNames">
<list>
<value>ConsoleLoggingBeforeAdvice</value>
</list>
</property>
</object>
</objects>
然而,當我試圖實現通過DefaultAdvisorAutoProxyCreator的相同的結果,一切正常,除了攔截器總是被創建爲Singleton(即使它被配置爲singleton =「false」)。在配置如下:
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net">
<object id="ConsoleLoggingBeforeAdvice" 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"/>
</objects>
現在,我怎麼能保證這兩個目標和相關的攔截器被視爲由DefaultAdvisorAutoProxyCreator的原型?
此問題也被張貼[在春季論壇](http://forum.springframework.net/showthread.php?9977-Using-Prototype-Interceptors-with-DefaultAdvisorAutoProxyCreator&p=29004#post29004) – Marijn 2012-02-17 09:57:21
是的,我是在那裏發佈它的人,但是這個問題也有一個配置問題,我後來解決了(並更新了這個帖子)。所以,如果出現錯誤配置問題,它會爲我工作,以便在線程範圍創建原型。但是,DefaultAdvisorAutoProxyCreator始終以單身形式創建攔截器的主要問題仍然處於打開狀態。 – ehsanullahjan 2012-02-18 15:59:43