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!
I am not sure if this can help but just sharing a few things which I have used.
For cyclic dependency one thing that I did when my war file had a src module in it. The referring module can just take the jar from the war by adding maven dep something as: (right now not sure about the exact syntax)
<dependancy> <groupid ../> <artifactid .war module name./> <jarModuleName> </dependancy>
In one case I could not call the src of the war module because of self redundancy. Then I had to make a separate src module and add it as a dependency to all the war modules where it was required.
I am not sure if this can help you. Just wanted to share what I knew. Let me know if you can improve on my comments.
You should split these dependencies a little bit more.
Something like this would be better:
BIZ
would depend onEJB
that depends onAPI
.Now this will solve your immediate problem but I would not recommend you to create those factories, you should use CDI instead but that's is a different story.