ehsanj
ehsanj

Reputation: 5660

How to Define Prototype Interceptors with DefaultAdvisorAutoProxyCreator in Spring.NET

I am new to Spring.NET and am just playing around trying different things out. As part of my testing, I created a simple object:

public interface ICommand {
    void Execute(object context);
}

with one implementation:

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

Finally, I've a simple before advice as follows:

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

As you can see, much of this stuff is from the Spring.NET quick start samples.

So, I configured the ServiceCommand to be wrapped in a ConsoleLoggingBeforeAdvice via ProxyFactoryObject and marked both the objects as prototype (see config below). This works as expected: each time we request a ServiceCommand, a new instance of both the object and associated interceptor is created:

<?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>

However, when I try to achieve the same results via DefaultAdvisorAutoProxyCreator, everything works except that the interceptor is always created as Singleton (even though it's configured as singleton="false"). The config is as follows:

<?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>

Now, how can I ensure that both the object and associated interceptor are treated as prototypes by DefaultAdvisorAutoProxyCreator?

Upvotes: 0

Views: 936

Answers (1)

ehsanj
ehsanj

Reputation: 5660

OK, I've figured out that setting InterceptorNames on DefaultAdvisorAutoProxyCreator will correctly instantiate interceptors as prototypes (if they're configured so). But this somehow feels incorrect as the DefaultAdvisorAutoProxyCreator should be able to pick interceptors from advisors and honor their configuration settings.

I am still not 100% clear on how to create prototype interceptors under different scenrarios. For example, all my attempts to create thread-scoped interceptors while using DefaultAdvisorAutoProxyCreator have failed.

Anyways, here's the xml config that works for me:

<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" default-autowire="constructor">
    <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">
        <property name="InterceptorNames" value="ConsoleLoggingBeforeAdvice"/>
    </object>
</objects>

I am totally confused with the idea of creating prototype interceptors. Are interceptors supposed to be or recommended to be prototypes at all or should they always be singletons?

Upvotes: 1

Related Questions