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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:39:54+00:00 2026-05-24T01:39:54+00:00

I am getting into Spring now and I have a question. Let’s assume we

  • 0

I am getting into Spring now and I have a question. Let’s assume we have an interface IControl and we have three implementations of it – e.g. Button, Label, Checkbox.
The user will enter a number N at runtime and I should create N controls of a specific type(Button, Label or Checkbox). The problem is I want this specific type to be configured before the program is run. For example I configure in my spring.xml that I want buttons, then the program is run, the user enters 10 and I create 10 buttons.

<beans>
   <bean id="controlCreator" class="org.company.ControlCreator">
       <property name="controlType" ref="?!?!?!?!?!?!"/>
   </bean>   
</beans>

How should I go configuring this?

P.S This is just a learning example I made up.

Best regards,
Petar

  • 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-24T01:39:55+00:00Added an answer on May 24, 2026 at 1:39 am

    First of all, allow me to state that Spring is not intended to do this. Spring wants to an unintrusive method of wiring your components together. It’s not meant to be used for spawning objects at runtime.

    The correct approach

    The correct approach would be to design a factory class. You can set the defaults of this using Spring at startup, then ask the factory for instances at runtime.

    The intrusive apporach

    If you really want to use Spring, you can do intrusively by asking the applicationContext for an instance of your control.

    By default, all Spring beans are singletons, but you can make them into prototypes.

    Disclaimer: code not tested, but should be something like this:

    Applicationcontext.xml

    <beans>
       <bean id="myButton" class="org.company.Control" scope="prototype">
           <property name="controlType" value="button" />
       </bean>   
    
       <bean id="controlCreator" class="org.company.ControlCreator" scope="singleton">
       </bean>   
    
    </beans>
    

    Code

    public class controlCreator implements ApplicationContextAware {
    
      private ApplicationContext appContext;
    
      public Control createButton(){
        // since this bean is prototype, a new one will be created like this
        return getApplicationContext().getBean("myButton");
      }
    
    
      // ... getter and setter for applicationContext
    
    }
    

    The unintrusive approach

    If you really want to use Spring, and you really want to use it unintrusively, you will have to use Method Injection. Inject a factory method into your class.

    The code would be the following:

    Applicationcontext.xml

    <beans>
       <bean id="myButton" class="org.company.Control" scope="prototype">
           <property name="controlType" value="button" />
       </bean>   
    
       <bean id="controlCreator" class="org.company.ControlCreator" scope="singleton">
           <lookup-method bean="myButton" name="createButton"/>
       </bean>   
    
    </beans>
    

    Code

    public class controlCreator implements ApplicationContextAware {
    
      private ApplicationContext appContext;
    
      public abstract Control createButton();
    
    
      // ... getter and setter for applicationContext
    
    }
    

    This will return the bean when the lookup method is called. Since the bean is prototype, it will create a new one.

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

Sidebar

Related Questions

I'm getting into more of a TDD workflow, and have a mix of MVC
I'm just getting into creating some WCF services, but I have a requirement to
I have just been getting into low level programming (reading/writing to memory that sort
I'm just getting into unit testing, and have written some short tests to check
I'm getting into dynamic data sites (remarkably powerful), and enjoying it. However, I have
Thinking about getting into .net technology project management I've had plenty of experience with
I'm getting into ASP.NET (C# - I know it doesn't matter for this particular
I am just getting into ibatis with SqlMap for the first time and I
I just started getting into BizTalk at work and would love to keep using
I'm just getting into the practice of version control (I'd like to use Eclipse

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.