maven循环依赖 [英] maven cyclic dependencies

查看:218
本文介绍了maven循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块化的maven项目,其中两个模块BIZ和EJB包含如下内容:

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();
    ....
    }

}

如您所见,EJB依赖于BIZ,因为它使用 MyClassX (事实上,它使用了几类BIZ)。这就是为什么 ImplFactory 使用反射来实例化 InterfaceImpl 的原因。问题是 cl.newInstance()将抛出 ClassCastException ,因为2个模块分别属于WAR和JAR(模块EJB编译指定type =ejb 并使用maven ejb插件)并使用不同的ClassLoaders(它在JBoss 7上运行)。另一方面, InterfaceImpl 无法移动到BIZ,因为它的作业需要 MyEJB1 ,这会引入循环依赖。

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.

这样的事情会更好:


.
├── 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 将取决于 EJB ,这取决于 API

BIZ would depend on EJB that depends on API.

现在这将解决您的问题,但我不建议您创建这些工厂,您应该使用 CDI ,但这是一个不同的故事。

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.

这篇关于maven循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆