Maven 循环依赖 [英] maven cyclic dependencies
问题描述
我有一个模块化的 Maven 项目,其中两个模块BIZ"和EJB"包含如下内容:
//BIZ"模块的一部分:公共接口 MyInterface{公共无效 foo();}………………………………………………………………………………………………………………………………………………………………………………公共类 ImplFactory{公共静态 MyInterface getInterfaceImplementation(){MyInterface ret=null;类cl = 空;尝试 {cl= (Class extends MyInterface>) Class.forName("InterfaceImpl");ret= cl.newInstance();}....退退;}………………………………………………………………………………………………………………………………………………………………………………公共类 MyClassX{公共静态无效 doSomethingX(){}}//EJB"模块的一部分:公共类 InterfaceImpl 实现 MyInterface@EJB私人 MyEJB1 ejb1;公共无效 foo(){ejb1.doSomething();}………………………………………………………………………………………………………………………………………………@无状态公共类 MyEJB1{public void doSomething(){...MyClassX.doSomethingX();....}}
如您所见,EJB"依赖于BIZ",因为它使用了MyClassX(实际上,它使用了多个 BIZ 类).这就是 ImplFactory 使用反射来实例化 InterfaceImpl 的原因.问题是 cl.newInstance() 会抛出一个 ClassCastException 因为这两个模块分别属于一个 WAR 和一个 JAR(模块EJB"被编译指定 type="ejb"并使用 maven ejb 插件)并使用不同的类加载器(它在 JBoss 7 上运行).另一方面,InterfaceImpl 不能移动到 BIZ,因为它需要 MyEJB1 来完成它的工作,这会引入循环依赖.
所以我的问题是:您将如何解决这种棘手的情况(以编程方式或通过更改配置)?我希望你能帮帮我!谢谢!
你应该更多地拆分这些依赖项.
这样的东西会更好:
<前>.├── 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.xmlBIZ
依赖于 API
的 EJB
.
现在这将解决您当前的问题,但我不建议您创建这些工厂,您应该使用 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.
这篇关于Maven 循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!