java - 如何解决maven循环问题?

标签 java maven-2 maven

我有这种依赖情况:

C (->) B -> A -> C

其中 B->C 表示模块 C 在其 pom.xml 中依赖于 B。

嗯...我想在 C 中使用 B 中的类,所以我必须将 C->B 但我收到一个循环错误,如您在上面看到的...

您看到任何解决方法吗?我无法将该类从 B 移到 C 中,因为它已经使用了 A 中的一些类,并且将再次循环。

我尝试在 C 中声明一个接口(interface) IAlfa 并在 B(Alfa) 中实现它(但是当我想使用它时,我需要实例化,如下所示:

IAlfa alfa = new Alfa() 

所以我再次需要从 B 导入 Alfa。

你觉得怎么样?

谢谢。

最佳答案

这不应该发生。我通常做的是这样的模块结构:

root
    - api (interfaces, enums and custom exceptions only)
    - impl (standard implementation of api -> dependency to api)
    - server (runtime dependency to impl, compile time to api only)
    - client (dependency to api only)

假设我们在 api 模块中有一个接口(interface) Person。现在,在 impl 模块中,您将有一个类 JpaPerson,因为 impl 使用 JPA。但是,这些是客户端和服务器现在不需要的详细信息,因此您不应该(并且在我的设计中不能)在 client 中执行 new JpaPerson()服务器。相反,您将有一个名为 PersonFactory 的接口(interface)(也在 api 内部)(当您谈论人时听起来很糟糕,也许将其命名为更人性化),它具有像这样的方法:

public interface PersonFactory{

    Person findPersonBySsn(String socialSecurityNumber);
    List<Person> findPersonsByName(String firstName, String lastName);
    List<Person> findPersonByBirthDate(Date birthDate);
    Person createPerson(
        String firstName, String lastName,
        Date birthDate, String socialSecurityNumber
    );

}

同样,在 impl 模块中实现此接口(interface),但仅在其他模块中将其作为接口(interface)引用。然后使用dependency injection将事物连接在一起(Spring、SEAM、EJB3,等等)。

您提到的情况是代码执行超出其职责范围的操作的结果(请阅读 separation of concerns )。

这种设计的巨大优势在于,您可以单独重构所有模块(api 除外),而无需更改其他模块中的代码。所以如果你想从jpa切换到经典hibernate,你只需要编辑impl。如果你想将服务器从axis切换到cxf,只需更改server即可。这让事情变得容易多了。当然,您不会获得循环依赖。

<小时/>

编辑:

一个简单(更简单)的解决方案是引入一个模块 d,它作为依赖项包含在 a、b 和 c 中。将所有通用代码移至 d。 (实际上将这个 d 重命名为 a,将 a,b,c 重命名为 b,c,d)。

关于java - 如何解决maven循环问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3353463/

相关文章:

java如何同步一组方法

java - 无法在 Java 中将 ArrayList 的 ArrayList 实例化为列表的列表

maven-2 - maven cobertura 报告 0% 与 aspectj

maven-2 - 即使另一个配置文件被激活,如何保持 activeByDefault 的 Maven 配置文件处于事件状态?

java - 多模块maven项目

java - 如何将 row_number (或具有相同行为的任何内容)与 Hibernate Criteria API 一起使用?

java - neo4j 服务器扩展示例 -- java.net.BindException : Address already in use

java - 供客户使用的单独模块

maven - 在 tomcat-server 中部署时与应用程序的依赖关系问题

java - Java字节码有 "indirect jump"吗?