maven cyclic dependencies

2020-03-26 05:45发布

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!

2条回答
贪生不怕死
2楼-- · 2020-03-26 06:10

I am not sure if this can help but just sharing a few things which I have used.

  1. 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>

  2. 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.

查看更多
成全新的幸福
3楼-- · 2020-03-26 06:36

You should split these dependencies a little bit more.

Something like this would be better:

.
├── pom.xml
├── my-api ("API")
|   ├── pom.xml
|   └── src
|       └── main
|           └── java
|               └── my
|                   └── package
|                       └── MyInterface.java
├── my-ejb ("EJB")
|   ├── pom.xml
|   └── src
|       └── main
|           └── java
|               └── my
|                   └── package
|                       └── InterfaceImpl.java
└── my-web ("BIZ")
    ├── pom.xml
    └── src
        └── main
            ├── java
            |   └── my
            |       └── package
            |           └── ImplFactory.java
            └── webapp
                └── WEB-INF
                    └── web.xml

BIZ would depend on EJB that depends on API.

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.

查看更多
登录 后发表回答