I'm trying to create a method which receives a class (any class) and return an instance of this class. (It's like "Spring"). How can I do having this code?
Main:
public static void main(String[] args) {
    Factory factoria = new Factory();
    Auto auto = factoria.getObject(Auto.class);
    System.out.println(auto.Motor);
}
Factory class (getObject is the method that I'm trying to do):
public Class<?> getObject(Class<?> clase) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
    Class<?> cls = Class.forName(clase.getName());
    Class<?> objetoRaiz = (Class<?>) cls.newInstance();
    // if @Component
    procesar(objetoRaiz);
    return objetoRaiz;      
}
 
Aucun commentaire:
Enregistrer un commentaire