在强制执行Java的分层架构 [英] Enforcing layered architecture in Java
问题描述
由于用Java编写的软件系统包括三个层次,A - > B - > C,即A层使用层B和B使用C层
Given a software system written in Java consisting of three layers, A -> B -> C, i.e. layer A uses layer B and B uses layer C.
我要确保一类一层只只对同一层或其直接依赖类的访问,即乙方应能够访问C,但不是A.也应该能够访问B,而不是C。
I want to make sure that a class of one layer only has access only to classes of the same layer or its direct dependency, i.e. B should be able to access C but not A. Also A should be able to access B but not C.
有一个简单的方法来执行这样的限制?理想的情况是我想日食如果试图访问错误层的类来一次抱怨了。
Is there an easy way to enforce such a restriction? Ideally I want eclipse to complain at once if one tries to access a class of the wrong layer.
目前该软件使用Maven的。因此,我试图把A,B和C到不同的Maven模块,并正确地声明依赖。这工作得很好,以prevent B来访问A,但不prevent一个访问℃。
The software currently uses maven. Therefore I tried to put A, B, and C into different maven modules and to declare dependencies properly. This works fine to prevent B to access A, but does not prevent A to access C.
接下来,我试图从依赖到B.这排除C现在也$从A到C.P。$ pvents接入但是现在我不再能够使用复制的依赖,收集所需要的运行时间都传递依赖。
Next I tried to exclude C from the dependency to B. This now also prevents access from A to C. However now I am no longer able to use copy-dependencies to collect all transitive dependencies needed for run time.
有没有可以让我的层完全分离的好方法,也可以让我去收集所有需要运行时依赖?
Is there a good way that allows me a clean separation of layers, but also allows me to collect all needed runtime dependencies?
推荐答案
也许你可以在A的POM试试这个:
Maybe you can try this in the pom of A:
<dependency>
<groupId>the.groupId</groupId>
<artifactId>moduleB</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>the.groupId</groupId>
<artifactId>moduleC</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>the.groupId</groupId>
<artifactId>moduleC</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
这能帮助你吗?
这篇关于在强制执行Java的分层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!