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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:29:49+00:00 2026-06-12T15:29:49+00:00

I have a problem with Spring: I need to reuse the same instance of

  • 0

I have a problem with Spring: I need to reuse the same instance of bean twice, but not making it singleton.

Here is a brief ApplicationContext:

<bean class="a.b.c.Provider" id="defaultProvider" scope="prototype">
    <constructor-arg ref="lifecycle" />
    <constructor-arg ref="propertySetter" />
</bean>

<bean name="lifecycle" class="a.b.c.Lifecycle" scope="prototype">
        <constructor-arg ref="someParam" />
        ... and more args
</bean>

<bean id="propertySetter" class="a.b.c.PropertySetter" scope="prototype">
    <constructor-arg ref="lifecycle" />
</bean>

So, I need to have fully initialized Provider with Lifecycle and PropertySetter inside,
and this PropertySetter must contain reference to same Lifecycle, as the Provider have.

When I define lifecycle and propertySetter as singletons, it causes big problems, because
if I create more than one Provider, all instances of Provider class shares same lifecycle
and property setter, and it’s breaking application logic.

When I try to define all beans as prototypes, Lifecycles in Provider and in PropertySetter are not the same => exceptions again.

I have one solution: to pass to Provider only Lifecycle and create PropertySetter inside Provider java constructor (by extending Provider).
It is working well, but only in my local environment. In production code I can’t extend 3pty Provider class, so I can’t use this solution.

Please advise me, what most appropriate to do in this situation?

  • 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-12T15:29:50+00:00Added an answer on June 12, 2026 at 3:29 pm

    You don’t need to extend Provider. Just create your own ProviderFactory that will take reference to lifecycle and will create PropertySetter and then Provider:

    public class ProviderFactory {
    
      public static create(Lifecycle lc) {
        return new Provider(lc, new PropertySetter(lc));
      }
    }
    

    Here is Spring declaration:

    <bean id="defaultProvider" scope="prototype" 
          class="a.b.c.ProviderFactory" factory-method="create">
        <constructor-arg ref="lifecycle" />
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my Spring Classes. I need to get all Beans
I have a problem where I need to split an input string into possible
I have small problem with Spring MVC. Basically what I'm trying to do is
I have the following problem with the timeouts in Spring Security with JSF: I've
Hi i am new to Spring and I have a following problem: I had
I have what seems like a simple problem. I have a Spring web app,
I'm facing following problem actually: I'm using Spring with jQuery. I have Controller: @Controller
I have a problem on the following situation: In my Spring, Hibernate application I
I need to abstract some behavioural code and have a problem trying to reference
I have following code, but validation for the custom type( UserDetails ) are not

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.