I have a modular maven project, in which two modules, "BIZ" and "EJB" contain somthing like this:
//PART OF "BIZ" Module:
public interface MyInterface{
public void foo();
}
............................................
public class ImplFactory{
public static MyInterface getInterfaceImplementation(){
MyInterface ret=null;
Class<? extends MyInterface> cl = null;
try {
cl= (Class<? extends MyInterface>) Class.forName("InterfaceImpl");
ret= cl.newInstance();
}
....
ret ret;
}
.......................................
public class MyClassX{
public static void doSomethingX(){
}
}
//PART OF "EJB" Module:
public class InterfaceImpl implements MyInterface
@EJB
private MyEJB1 ejb1;
public void foo(){
ejb1.doSomething();
}
........................................
@Stateless
public class MyEJB1{
public void doSomething(){
...
MyClassX.doSomethingX();
....
}
}
As you see, "EJB" depends on "BIZ" as it uses MyClassX (in the truth, it uses several classes of BIZ). This is the reason why ImplFactory uses reflection to instantiate InterfaceImpl. The problem is cl.newInstance() will throw a ClassCastException as the 2 modules belong respectively to a WAR and a JAR (module "EJB" is compiled specifying type="ejb" and using the maven ejb plugin) and use different ClassLoaders (it runs on JBoss 7). On the other hand, InterfaceImpl cannot be moved to BIZ as it needs MyEJB1 for its job and this would introduce a cyclic dependency.
So my question is: how would you solve this tricky situation (either programmatically or by changing the configuration)? I hope you can help me! Thanks!