Results 1 to 3 of 3

Thread: Using Prototype Interceptors with DefaultAdvisorAutoProxyCreator

  1. #1
    Join Date
    Feb 2012
    Location
    Canton, MI
    Posts
    12

    Default Using Prototype Interceptors with DefaultAdvisorAutoProxyCreator

    Hi, I am trying to create an ITargetSourceCreator for kicks and then configure it to be used by DefaultAdvisorAutoProxyCreator via the CustomTargetSourceCreatorProperty (in XML config) like so:

    HTML Code:
    <?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 id="PrototypeTargetSourceCreator" type="Spring.Examples.AopQuickStart.PrototypeTargetSourceCreator"/>
    
        <object type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator">
            <property name="CustomTargetSourceCreators">
                <list element-type="string">
                    <value>PrototypeTargetSourceCreator</value>
                </list>
            </property>
        </object>
    </objects>
    The primary reason I am doing this is to address the issue posted in this thread. While someone can hopefully help me on that quesiton, I thought I'll do more experiments. And as part of those experiments, I've run into something that may potentially be an issue with the framework code.

    For reference, my implementation of the ITargetSourceCreator is as follows:

    Code:
    public class PrototypeTargetSourceCreator : AbstractPrototypeTargetSourceCreator {
        protected override AbstractPrototypeTargetSource CreatePrototypeTargetSource(Type objectType, string name, IObjectFactory factory) {
            Console.WriteLine(objectType);
            Console.WriteLine(name);
            Console.WriteLine(factory);
    
            if (factory.ContainsObject(name) && factory.IsPrototype(name)) {
                return new PrototypeTargetSource();
            }
    
            return null;
        }
    }
    Now, when I run the application, I get an exception in Spring.Aop.Framework.AutoProxy.AbstractAutoProxyCr eator at line 425. The actual line of code is:

    Code:
    ITargetSourceCreator tsc = (ITargetSourceCreator)customTargetSourceCreators[i]
    and here's the stack trace:

    System.TypeInitializationException: The type initializer for 'Spring.Examples.AopQuickStart.ObjectRegistry' threw an exception. ---> Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'PrototypeTargetSourceCreator' defined in 'file [C:\Users\ejan\Learning\PracticeBench\Spring.NET\Sp ring.Examples.AopQuickStart\bin\Debug\Objects.xml] line 11' Initialization of object failed : Unable to cast object of type 'System.String' to type 'Spring.Aop.Framework.AutoProxy.ITargetSourceCreat or'. ---> System.InvalidCastException: Unable to cast object of type 'System.String' to type 'Spring.Aop.Framework.AutoProxy.ITargetSourceCreat or'.
    at Spring.Aop.Framework.AutoProxy.AbstractAutoProxyCr eator.GetCustomTargetSource(Type*objectType,*Strin g*name) in AbstractAutoProxyCreator.cs: line 425
    at Spring.Aop.Framework.AutoProxy.AbstractAutoProxyCr eator.PostProcessBeforeInstantiation(Type*objectTy pe,*String*objectName) in AbstractAutoProxyCreator.cs: line 655
    at Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.ApplyObjectPostProcessorsBeforeI nstantiation(Type*objectType,*String*objectName) in AbstractAutowireCapableObjectFactory.cs: line 330
    at Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.InstantiateObject(String*name,*R ootObjectDefinition*definition,*Object[]*arguments,*Boolean*allowEagerCaching,*Boolean*sup pressConfigure) in AbstractAutowireCapableObjectFactory.cs: line 896
    --- End of inner exception stack trace ---
    at Spring.Objects.Factory.Support.AbstractAutowireCap ableObjectFactory.InstantiateObject(String*name,*R ootObjectDefinition*definition,*Object[]*arguments,*Boolean*allowEagerCaching,*Boolean*sup pressConfigure) in AbstractAutowireCapableObjectFactory.cs: line 938
    at Spring.Objects.Factory.Support.AbstractObjectFacto ry.CreateAndCacheSingletonInstance(String*objectNa me,*RootObjectDefinition*objectDefinition,*Object[]*arguments) in AbstractObjectFactory.cs: line 2144
    at Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObjectInternal(String*name,*Type*requiredTyp e,*Object[]*arguments,*Boolean*suppressConfigure) in AbstractObjectFactory.cs: line 2065
    at Spring.Objects.Factory.Support.AbstractObjectFacto ry.GetObject(String*name) in AbstractObjectFactory.cs: line 1826
    at Spring.Objects.Factory.Support.DefaultListableObje ctFactory.PreInstantiateSingletons() in DefaultListableObjectFactory.cs: line 505
    at Spring.Context.Support.AbstractApplicationContext. Refresh() in AbstractApplicationContext.cs: line 1017
    at Spring.Context.Support.XmlApplicationContext..ctor (XmlApplicationContextArgs*args) in XmlApplicationContext.cs: line 111
    at Spring.Context.Support.XmlApplicationContext..ctor (String[]*configurationLocations) in XmlApplicationContext.cs: line 128
    at Spring.Examples.AopQuickStart.ObjectRegistry..ccto r() in ObjectRegistry.cs: line 6
    --- End of inner exception stack trace ---
    at Spring.Examples.AopQuickStart.ObjectRegistry.Resol ve<T>(String*name)
    at Spring.Examples.AopQuickStart.Program.Main() in C:\Users\ejan\Learning\PracticeBench\Spring.NET\Sp ring.Examples.AopQuickStart\Program.cs: line 8
    So it looks like it's directly converting the string "PrototypeTargetSourceCreator" (from XML config) to ITargetSourceCreator. I think it should use the IObjectFactory to resolve this name to an ITargetSourceCreator instance.

    Besides, I couldn't find any concrete implementations of ITargetSourceCreator which suggests this may be a work in progress. But most certainly, I think the fact that we don't have any concrete ITargetSourceCreator implementations is the reason for the issue I mentioned above.

    Any thoughts?

  2. #2
    Join Date
    Feb 2012
    Location
    Canton, MI
    Posts
    12

    Default

    Ok, here too it was me doing things incorrectly. So, the way I configured the ICustomTargetSourceCreator with the DefaultAdvisorAutoProxyCreator was incorrect (I was registering it as a string expecting magic to happen -- but magic doesn't happen in software development, does it?):

    HTML Code:
    <object type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator">
        <property name="CustomTargetSourceCreators">
            <list element-type="string">
                <value>PrototypeTargetSourceCreator</value>
            </list>
        </property>
    </object>
    The correct way is to configure my custom target source creator explicitly as an ITargetSourceCreator object:

    HTML Code:
    <object type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator">
        <property name="CustomTargetSourceCreators">
            <list element-type="Spring.Aop.Framework.AutoProxy.ITargetSourceCreator">
                <object type="Spring.Examples.AopQuickStart.ThreadLocalTargetSourceCreator"/>
            </list>
        </property>
    </object>
    Once I did this, it worked perfectly without any issues.

    - janlala

  3. #3
    Join Date
    Feb 2012
    Location
    Canton, MI
    Posts
    12

    Default

    Just to make it clear, now the program is working (which previously was not) but I still am not 100% clear on how to create prototype interceptors. I can use the InterceptorNames property on DefaultAdvisorAutoProxyCreator and that works sometimes but I am not 100% clear on how it really interacts with the advisors in different scenarios (like what if I am using a ThreadLocalTargetSource as value of the Advice property on the Advisor). I am totally confused with the idea of creating prototype interceptors.

    Are interceptors supposed to be or recommended to be prototypes?

    As mentioned above, the following xml produces prototype interceptors but as mentioned earlier I'm unable to make it work with ThreadLocalTargetSource. I don't really need it for anything, I'm just exploring various options:

    HTML Code:
    <?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>
    - janlala
    Last edited by janlala; 02-18-2012 at 06:17 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •