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

  • SEARCH
  • Home
  • 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 9235893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:09:36+00:00 2026-06-18T07:09:36+00:00

I have come across a scenario as below: MyBean – Defined in XML Configuration.

  • 0

I have come across a scenario as below:

MyBean – Defined in XML Configuration.

I need to inject MyBean into multiple threads.
But my requirements is:
1) The reference retrieved in two different threads should be different
2) But I should get the same reference irrespective how many times I retrieve bean from single thread.

Eg :

Thread1 {

    run() {
        MyBean obj1 = ctx.getBean("MyBean");
        ......
        ......
        MyBean obj2 = ctx.getBean("MyBean");
    }
}

Thread2 {

    run(){
        MyBean obj3 = ctx.getBean("MyBean");
    }
}

So Basically obj1 == obj2 but obj1 != obj3

  • 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-06-18T07:09:37+00:00Added an answer on June 18, 2026 at 7:09 am

    You can use the custom scope named SimpleThreadScope.

    From the Spring documentation :

    As of Spring 3.0, a thread scope is available, but is not registered
    by default. For more information, see the documentation for
    SimpleThreadScope. For instructions on how to register this or
    any other custom scope, see Section 3.5.5.2, “Using a custom
    scope”
    .

    Here an example of how to register the SimpleThreadScope scope :

    Scope threadScope = new SimpleThreadScope();
    beanFactory.registerScope("thread", threadScope);
    

    Then, you’ll be able to use it in your bean’s definition :

    <bean id="foo" class="foo.Bar" scope="thread">
    

    You can also do the Scope registration declaratively :

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
        <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
            <property name="scopes">
                <map>
                    <entry key="thread">
                        <bean class="org.springframework.context.support.SimpleThreadScope"/>
                    </entry>
                </map>
            </property>
        </bean>
    
        <bean id="foo" class="foo.Bar" scope="thread">
            <property name="name" value="bar"/>
        </bean>
    
    </beans>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have come across a scenario where we need to track the setting and
I have come across a unusual scenario where I need to turn a query
I have come across a scenario where I need to cast the output of
I am looking into TDD and I have come across a scenario that I
Why would you need to overload the [] operator? I have never come across
I often come across a scenario where I have two collections of objects (either
Through the years I've come across this scenario more than once. You have a
I have come across a strange scenario using the entity framework in the Where
I'm brand new to MVC and have just come across a scenario that I
I've come across a scenario where I need to return a complex set of

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.