简体   繁体   English

如何在eclipse中解决项目中不同模块之间的循环依赖?

[英]How to solve cyclic dependency between different modules in a project in eclipse?

The Problem log in eclipse shows "A cycle was detected in the build path of the project ...." Any idea what to do to get rid of these cyclic dependencies? eclipse中的问题日志显示“在项目的构建路径中检测到一个循环......”任何想法如何摆脱这些循环依赖? I don't know which projects are dependent on each other. 我不知道哪些项目相互依赖。

您可以在eclipse中调整循环依赖关系严重性:

Preferences > Java > Compiler > Building > Build path problems > Circular dependencies

In project setting you can see the dependencies, what you could do is removing all dependencies and add one dep after another. 在项目设置中,您可以看到依赖项,您可以做的是删除所有依赖项并逐个添加一个dep。 A cycle means you shouldn't have done it that way. 循环意味着你不应该这样做。 Your solutions after identifying the classes, let says your big Project A is requiring Class CB1 from secondary Project B and CB1 is requiring class CA1 from project A : 在确定类之后的解决方案,让你的大项目A要求来自二级项目B的CB1类,而CB1要求项目A中的类CA1:

  • Move a class CB1 in the project A to remove the dependency --> works if it doesn't create more dependencies. 移动项目A中的类CB1以删除依赖项 - >如果它不创建更多依赖项,则可以工作。
  • extract interfaces and use non dependant interfaces (you might need to create super class or more interfaces depending of your structure). 提取接口并使用非依赖接口(您可能需要根据结构创建超类或更多接口)。
  • implement differently, why do you specifically need class from other project? 实现不同,为什么你特别需要来自其他项目的类? try using common parent classes if any 尝试使用常见的父类(如果有)

There are three ways to remove cyclic dependency between projects in eclipse, 在eclipse中有三种方法可以消除项目之间的循环依赖关系,

1.Go to project-> java compiler-> building -> Enable project specific settings. 1.转到project-> java compiler-> building - >启用项目特定设置。
Select build path problems and give warning as option for circular dependency. 选择构建路径问题并将警告作为循环依赖项的选项。

2.Go to project->java build path. 2.转到project-> java build path。 In projects tab, select the project and remove. 在“项目”选项卡中,选择项目并删除。

3.Go to your META-INF folder, open MANIFEST.MF. 3.转到META-INF文件夹,打开MANIFEST.MF。 In your MANIFEST.MF view tab, you can see the cyclic dependent project in "Import package:" column. 在MANIFEST.MF视图选项卡中,您可以在“导入包:”列中查看循环相关项目。
Remove the project from the column. 从列中删除项目。

The first option does not really resolve the error. 第一个选项并不能真正解决错误。 The second and the third option are the right way to resolve this dependency. 第二个和第三个选项是解决此依赖关系的正确方法。

A cyclic dependency in eclipse indicates that there is a cycle in the buildpaths between projects in Eclipse. eclipse中的循环依赖表明Eclipse中的项目之间的构建路径中存在一个循环。

So if you have 5 projects, say A, B, C, D and E, then a cyclic dependency could be that: 因此,如果你有5个项目,比如A,B,C,D和E,那么循环依赖可能是:

  1. A requires B in its build-path A在其构建路径中需要B.
  2. B requires D in its build-path B在其构建路径中需要D.
  3. D requires A in its build-path D在其构建路径中需要A.

Hence A->B->D->A is a cycle. 因此A->B->D->A是一个循环。

Because of this cycle, Eclipse does not know which project to compile first. 由于这个循环,Eclipse不知道首先编译哪个项目。

You need to refactor your code to remove this cyclic dependency. 您需要重构代码以删除此循环依赖项。 Or if the actual code doesn't have such dependencies, remove the build-path entries which are unnecessary. 或者,如果实际代码没有此类依赖项,请删除不必要的构建路径条目。

To find out the dependencies between plain Java projects, go see Project Properties | 要找出普通Java项目之间的依赖关系,请参阅Project Properties | Java Build Path | Java构建路径| Projects. 项目。 If you have J2EE projects, there is an additional setting in Project Properties | 如果您有J2EE项目,Project Properties中还有一个附加设置 Project References. 项目参考。 Check those for all your projects, build the graph on a piece of paper (or screen) and see what you can do to break the cycle. 检查所有项目的那些,在一张纸(或屏幕)上构建图表,看看你可以做些什么来打破周期。

Usually, a healthy dependency graph has the form of a star, with a core project containing common resources, and each leaf project implementing some specific user functionality. 通常,健康的依赖关系图具有星形的形式,核心项目包含公共资源,每个叶项目实现一些特定的用户功能。 In detail, the core may in fact consist of several interdependent projects; 具体而言,核心实际上可能包括几个相互依存的项目; same for larger leafs. 对于较大的叶子也是如此。

A typical way out of a dependency cycle is the merging of several closely coupled projects. 脱离依赖循环的典型方法是合并几个紧密耦合的项目。

You might have to refactor your projects or the code itself. 您可能必须重构您的项目或代码本身。 It might be an issue with the packaging with you application. 与您的应用程序打包可能是一个问题。

I had run into this issue a long time ago, and even though I've forgotten how I resolved that one, when it came to communicating from a class in the required project to one in the requiring project, I had the sendee install a java.lang.reflect.Method object in the sender, where it could be invoked without declaring the sendee class within the sender's code. 很久以前我遇到过这个问题,即使我忘记了我是如何解决这个问题的,当谈到从所需项目中的一个类到需求项目中的一个类时,我让sendee安装了一个java发送方中的.lang.reflect.Method对象,可以在不声明发送方代码中的服务类的情况下调用它。

I'm sure there are preferred ways of doing this, such as having the sender and sendee share an interface, but in terms of coding economy, this seems to be the simplest. 我确信有这样做的首选方式,例如让发送者和服务者共享一个界面,但在编码经济方面,这似乎是最简单的。

As other answers seemed to suggest, this does not appear to be a true circularity, but rather an artifact of the Eclipse design. 正如其他答案似乎暗示的那样,这似乎不是真正的循环,而是Eclipse设计的神器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM