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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:46:14+00:00 2026-05-27T08:46:14+00:00

A common pattern I am falling into with my beans is as follows: public

  • 0

A common pattern I am falling into with my beans is as follows:

public class PrototypeBean {
    private SingletonBean singletonBean;
    private int value;
    public PrototypeBean(int value, singletonBean) {
        this.value = value;
        this.singletonBean = singletonBean;
    }
}

I cannot for the life of me figure out what is the cleanest way to instantiate a prototype bean like this if I do not know what value will be until runtime. My current pattern is as follows:

public class FactoryImpl implements Factory{
    private SingletonBean singletonBean;

    public FactoryImpl(SingletonBean singletonBean) {
       this.singletonBean = singletonBean;
    }

    public PrototypeBean getPrototypeBean(int value) {
       return new PrototypeBean(value, singletonBean);
    }
}

Where Factoryimpl is wired together in XML. This still seems horrible to me and feels like I am breaking IoC. Is there a cleaner way of doing this in spring?

  • 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-27T08:46:14+00:00Added an answer on May 27, 2026 at 8:46 am

    This is one of the most wanted features missing in Spring I came across. Basically your solution is the best workaround I can think of (just make singletonBean and value fields final).

    Another approach would be to let Spring create PrototypeBean with only one constructor argument (SingletonBean) and passing value afterwards (see: Best way to refactor this in Spring?). However this is less clean as it allows one to create half-initialized object.

    The best solution would be to take advantage of parametrized <lookup-method/> – feature that waits to be implemented in Spring: SPR-7431. Don’t forget to vote and comment!

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

Sidebar

Related Questions

A common pattern in C++ is to create a class that wraps a lock
This is becoming a common pattern in my code, for when I need to
I'm running into a common pattern in the code that I'm writing, where I
What is an ObjectMother and what are common usage scenarios for this pattern?
This is a common pattern among apps and I am sure has a graceful
Calling all JS historians. A common pattern is to alias a class' prototype to
This is probably a common pattern in coding, though I am not knowledgeable enough
<td><input type=submit id=view_{{ var }} value=View></td><td> I need to catch this id pattern. I
I don't remember exactly is it a common pattern but I have a class
This seems to be a pretty common pattern: for row in reader: c1=row[0] if

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.