且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven 循环依赖

更新时间:2022-12-15 08:51:26

你应该更多地拆分这些依赖项.

这样的东西会更好:

.├── pom.xml├── my-api ("API")|├── pom.xml|└── src|└── 主要|└── 爪哇|└── 我的|└── 包装|└── MyInterface.java├── my-ejb ("EJB")|├── pom.xml|└── src|└── 主要|└── 爪哇|└── 我的|└── 包装|└── InterfaceImpl.java└── my-web ("BIZ")├── pom.xml└── src└── 主要├── java|└── 我的|└── 包装|└── ImplFactory.java└── 网页应用└── 网络信息└── web.xml

BIZ 依赖于 APIEJB.

现在这将解决您当前的问题,但我不建议您创建这些工厂,您应该使用 CDI 但那是另一回事了.

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!

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.