javabeans - How to add multiple beans to a list and iterate through the list in java? -


so, have class have injected multiple beans, like

private class myclass{     @resource(name = "bean1")     private bean1 bean1obj;     @resource(name="bean2")     private bean2 bean2obj;     @resource(name="bean3")     private bean3 bean3obj; } 

i need particular method in beans (say callmethod) executed in class. require list add beans list , iterate through execute or define list in xml file , iterate through it. not sure , how that. how add these beans arraylist , iterate through it?

adding beans list easy, challenge here (presuming using spring) want after dependency injection occurs.

there's way that, via @postconstruct.

presuming each bean class has common superclass bean, try this:

public class myclass{     @resource(name = "bean1")     private bean1 bean1obj;     @resource(name = "bean2")     private bean2 bean2obj;     @resource(name = "bean3")     private bean3 bean3obj;      list<bean> beans = new arraylist<bean>();      @postconstruct     public void init() {         beans.add(bean1obj);         beans.add(bean2obj);         beans.add(bean3obj);     } } 

Comments