Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to configure the Cofiguration file in spring if we want to decide at run time which child object to instantiate?
    primarykey
    data
    text
    <pre><code>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; } } </code></pre> <p>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 ?</p> <p><strong>Edit:-</strong> </p> <p>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 </p> <pre><code>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 } </code></pre> <p>}</p> <p>Below is the code snippet from config file </p> <pre><code>&lt;bean id="myfactory" class="package.MyFactory" factory-method="getObject"&gt; &lt;/bean&gt; </code></pre> <p>// 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</p> <p>//Below is my client method</p> <pre><code>public static void main(String arrgs[]) { ParentClass pc=(ParentClass)XMLBeanFactor.getbean("myfactory");// Please ignore the syntax just consider the logic } </code></pre> <p><strong>Now how i will pass the value of i from main method to factory method considering above example?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload