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?
You should use a spring factory.
There are different flavors of factories
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.
–