Take the 2-minute tour ×
public Class MyClass1{

private ParentClass1 parentInstance;

private int i=1;

public MyClass1(ParentClass1  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 ?


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)
return childclass1;// will get from config file

return childclass2;// will get from config file




Below is the code snippet from config file

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

// 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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.


share|improve this answer
Hi Ralph, could you elaborate little bit how spring factory will help in above scenario. Tried the link but could not get related Facts. –  M Sach Aug 28 '11 at 15:44
Mohit Gupta: see my extended answer –  Ralph Aug 29 '11 at 6:26
Thanks Ralph.I got your point that we have to use approach where we should factory class alongwith spring. I will be grateful if you can give some example of it here considering the original post. Sorry for troubling you. But i am in urgent need of this.Thanks in advance. –  M Sach Aug 30 '11 at 17:15
Would it be too difficult to create one of each and fetch the desired one when needed? I think not. –  Tony Aug 31 '11 at 3:32
I got the static and non static flavour of factories in spring.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. –  M Sach Aug 31 '11 at 18:46
add comment

Your Answer


