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 8737091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:26:16+00:00 2026-06-13T10:26:16+00:00

I have a singleton Spring bean (and it has to stay a singleton) which

  • 0

I have a singleton Spring bean (and it has to stay a singleton) which needs a fresh instance of another bean (Lets call it X) every time a certain method executes.

So far I looked at the following approaches:

  • just create X using new. This worked for a while but now we need spring AOP features for X, so this doesn’t work anymore, since the resulting instances are not Spring managed.

  • I considered a FactoryBean as a dependency, but I would only get a single X instance from the FactoryBean, which doesn’t meet my first instance.

  • the current plan is to manually lookup X in the Spring context, and declare it there with a prototype dependency. This should work, but I think it is really ugly.

=> How can I inject a factory in my bean so that I can call its factory method any time I see fit and getting a spring managed instance out of it.

  • 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-13T10:26:17+00:00Added an answer on June 13, 2026 at 10:26 am

    The means of choice for a scenario like this is called lookup method injection. In short, this uses the approach of a call to a bean method resulting in a new bean instance created. You’d start by creating a class with an abstract method that will eventually provide the dependency instance:

    abstract class MyClient implements Client {
    
      void businessMethod(…) {
    
        Dependency dependency = getDependencyInstance();
        …
      }
    
      abstract Dependency getDependencyInstance();
    }
    

    You now go ahead and configure a prototype bean definition for the dependency:

    <bean id="dependency" class="….DependencyImpl" scope="prototype" />
    

    As well as the client using the lookup-method element to always get a fresh instance of the dependency for each method call:

    <bean class="….MyClient">
      <lookup-method name="getDependencyInstance" bean="dependency" />
    </bean>
    

    This will cause a CGLib proxy being created for MyClient and the method declaration of getDependencyInstance(…) being backed by a TargetSource with a reference to the BeanFactory and the name of the bean to be looked up. On each method invocation the bean lookup will be triggered and a fresh instance of the prototype configured bean is returned.

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

Sidebar

Related Questions

Am new to Spring.I have a spring bean countService which is a singleton public
I have developed an utility class for spring which is a singleton which will
I have a singleton as follows, which creates an instance of NSDictionary to hold
I have a Singleton which has an NSMutableArray containing a Class, the Class contains
Assume that we have Spring bean UserController with singleton scope. All my further reasoning
I have a Spring MVC app where I am using a singleton bean that
I have a singleton class which I would like to be created using Spring's
I have a singleton spring bean that is being invoked in response to some
I have a manager as Spring wired bean. I believe every bean defined for
I have a singleton object that use another object (not singleton), to require some

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.