user3272931 user3272931 - 6 months ago 26
Java Question

Spring load Implementation no xml

I have the next question, I have a SPRING app, with a configuration xml (no Spring), this document has the name of the class to implement in my app (lets said com.stack.impl.MyClass) and this classs implements my Interface (lets said MyInterface)
So I want to dinamically call the implementation when something happen, like this:

String myClass=xml.getProperty(id);
MyInterface myInterface=ctx.getBean(myClass);
myInterface.execute();


Any sugestion? Thanks and regards

Answer

I used this method, thank alot for your help @duffymo and @jjhavokk, hope you get the same help from other ppl:

public AnnotationConfigApplicationContext init(){
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    try { 
        Class<DataPreProcessor> c= (Class<DataPreProcessor>) Class.forName("com.banamex.middleware.commons.DataPrepProcesorTest");
        Class<DataPreProcessor> c2= (Class<DataPreProcessor>) Class.forName("com.banamex.middleware.commons.DataPrepProcesorTest2");
         DataPreProcessor dp=c.newInstance();
        DataPreProcessor dp2=c2.newInstance();
        context.getBeanFactory().registerSingleton("c", dp);
        context.refresh();
    } catch (InstantiationException e) {

        e.printStackTrace();
    } catch (IllegalAccessException e) { 
        e.printStackTrace();
    } catch (ClassNotFoundException e) { 
        e.printStackTrace();
    } 
    return context;
}