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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:52:02+00:00 2026-05-25T02:52:02+00:00

public Class MyClass1{ private ParentClass1 parentInstance; private int i=1; public MyClass1(ParentClass1 instance) { this.parentInstance=instance;

  • 0
public Class MyClass1{

private ParentClass1 parentInstance;

private int i=1;

public MyClass1(ParentClass1  instance)
{
this.parentInstance=instance;

}

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}

}

Now we have five child class which extends ParentClass1. Now in spring when are composing MyClass1 ,how we will determine
which child instance out of five needs to be injected here (as it depends upon some runtime parameter depending on which
we have to create child instance like if i=1 instantiate child1, if i=2 instantiate child2). Please Guide me if there is any
way to configure the configuration file i.e spring-config.xml for above scenario ?

Edit:-

My question is how we will pass the argument in factory method .Assuming this parameter is coming from some value user selected on user interface and not know while setting up config file. Below is My factory, client and configuration file

Public class MYFactory
{
Public static getObject(int i)
{
if(i==1)
{
return childclass1;// will get from config file
}


if(i==2)
{
return childclass2;// will get from config file
}

//continued

}

}

Below is the code snippet from config file

<bean  id="myfactory"  class="package.MyFactory"  factory-method="getObject">
</bean>

// i know we can provide constructor argument above but that will be static. This argument is supposed to come from value user selected on user interface

//Below is my client method

public static void main(String arrgs[])
{
ParentClass pc=(ParentClass)XMLBeanFactor.getbean("myfactory");// Please ignore the                 syntax just consider the logic
}

Now how i will pass the value of i from main method to factory method considering above example?

  • 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-25T02:52:02+00:00Added an answer on May 25, 2026 at 2:52 am

    You should use a spring factory.

    There are different flavors of factories

    • static methods
    • instance methods
    • @Bean

    How it works:

    You USE the bean based on its super class or interface like any other bean. But the factory decide what exact subclass will be created. So when Spring need to inject one of the beans in an other bean, it will consult the factory for the concrete bean (or depending on the scope (Singleton) use the one that is already created). So the factory can decide by some logic which concrete subclass must be created.


    My mobile phone can not writ comments on stackoverflow gui, so I need to extend the answer to answer the comment: sorry I can not give an example because I am now (since 5h) in holiday for the next weeks. I have no PC and my phone can even not add an comment. So you need to ask an new question about the details.


    i do not understand york lasr comment. But the three-dimensional differentialgleichungen types in my answer are links to the recerence documentatin containinv an example for da
    each. BTW. i am will not havel internet for the next days, you should ask a new question if the reference is not enougth.

    –

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

Sidebar

Related Questions

I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have the following class public class MyClass { private int myAttr; public void
I have a List<MyClass> The class is like this: private class MyClass { public
I have a class like this: Public Class MyClass Private _intList As New List(Of
Suppose I have a class public class MyClass { private Set<String> set = new
This isn't valid code: public class MyClass { private static boolean yesNo = false;
I have a class that looks like the following: public class MyClass { private
public class MyClass { public int Age; public int ID; } public void MyMethod()
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have a: public class MyClass { private string PrivateString; } and I have

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.