I've an EAR project structure:
EAR
-
TestDependency.jar
- MyInterface.class;
-
TestEJBModule.jar
- ExternalObject.class (implements MyInterface);
-
TestWar
- SingletonBean
- MyTestClass (implements MyInterface);
In Singleton bean I need to instantiate ExternalObject class dinamically using the class name, but i get
Caused by: javax.ejb.EJBException: java.lang.ClassCastException: com.bsssrl.testejbmodule.ExternalObject cannot be cast to com.bsssrl.MyInterface
The code is:
Class localclass = Class.forName("com.bsssrl.test.testwar.MyTestClass");
Object olocal = localclass.newInstance();
MyInterface local = (MyInterface) olocal;
LOG.log(Level.INFO, "Test local.getString() : {0}",local.getString());
LOG.log(Level.INFO, "Test local.getAlfa() : {0}",local.getAlfa());
Class exclass = Class.forName("com.bsssrl.testejbmodule.ExternalObject");
Object oex = exclass.newInstance();
if(MyInterface.class.isAssignableFrom(exclass)){
MyInterface extern = (MyInterface) oex;
LOG.log(Level.INFO, "Test extern.getString() : {0}",extern.getString());
LOG.log(Level.INFO, "Test extern.getAlfa() : {0}",extern.getAlfa());
}else{
LOG.log(Level.SEVERE, "Class {0} not implement MyInterface",exclass);
}
The class is :
public class ExternalObject implements MyInterface{
public Alfa alfa ;
@Override
public String getString() {
return "from external object in EJB module";
}
@Override
public Alfa getAlfa() {
return alfa;
}
@Override
public void setAlfa(Alfa a) {
this.alfa = a;
}
}
The first piece of code works, but the second launch ClassCastException; I suppose I have a problem with classloader.
How can I solve it?
PS : I use wildfly-9.0.2.Final
Aucun commentaire:
Enregistrer un commentaire