How to configure the Cofiguration file in spring, if we want to decide at runtime which child object to instantiate?

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 classes that extend ParentClass1. Now in spring, when compiling MyClass1, how will we determine which of the five child instances to enter here (since this depends on some runtime parameter, depending on which we should create the child instance, for example, if I = 1 create an instance of child1 if i = 2 create an instance of child2). Please guide me if there is a way to configure ie spring -config.xml configuration file for the above script?

Edit: -

My question is how do we pass the argument in the factory method. At the same time, this parameter starts from a certain value selected by the user in the user interface and does not know when configuring the configuration 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 a snippet of code from the configuration file

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

// I know that we can provide the constructor argument above, but it will be static. This argument must come from the value of the user selected in the 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 do I pass the value of i from the main method to the factory method, looking at the example above?

+1
source share
1 answer

You must use spring factory .

There are different tastes of factories

How it works:

USE a bean based on your superclass or interface, like any other bean. But factory determines which subclass will be created. Therefore, when Spring needs to introduce one of the beans into another bean, it will consult the factory for a specific bean (or, depending on the area (Singleton), use one that has already been created). Thus, the factory can decide by some logic which particular subclass should be created.


My mobile phone cannot write comments on the stackoverflow gui, so I need to extend the answer to respond to the comment: sorry, I can’t give an example, because I’m on vacation for the next weeks now (from 5 hours). I do not have a PC, and my phone cannot even add a comment. So you need to ask a new question about the details.


I do not understand york lasr comment. But the three-dimensional differential types in my answer are links to a recerence documentatin document containing an example for da each. BTW. I will not be an Internet hub in the next days, you must ask a new question if the link is not enougth.

-

+1
source

Source: https://habr.com/ru/post/1369385/


All Articles