Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7519127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:51:06+00:00 2026-05-30T01:51:06+00:00

I want to create a thread-local object (with interceptors) using DefaultAdvisorAutoProxyCreator. I know how

  • 0

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>

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

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).

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:51:07+00:00Added an answer on May 30, 2026 at 1:51 am

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a background thread that's owned by an object. When that
I want to create a separate thread that runs its own window. Frankly, the
I have multi-thread application that I want to create a thread with different user
I want to create a thread for some db writes that should not block
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
When Using a thread-local database connection, closure of the connection is required when the
I want to create a project in which it is required to synchronize local

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.