可以在Java中强制执行程序包依赖关系层次结构吗? [英] Possible to enforce package dependency hierarchy in Java?

查看:97
本文介绍了可以在Java中强制执行程序包依赖关系层次结构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有包裹...

com.me.application
com.me.application.thing
com.me.library

我想强制执行以下规则:除了com.me.application.thing中的内容之外,com.me.application中的任何内容都不能包含com.me.library.

在Java代码,Maven,类路径或Java的任何其他等效于C链接的其他层上,这是否可能?

解决方案

我能想到的唯一方法是通过 AspectJ .

AspectJ是一种面向方面的语言,用于通过单独的编译过程将横切关注点添加到您的应用程序中. AspectJ的经典用途之一是执行策略,它适合您的情况.

基本上,您声明一些规则,这些规则决定从哪个包中调用哪个代码,并在遇到违反这些规则的方法调用(或在您的情况下为变量声明)时引发编译错误.您可以在精美的书 AspectJ in Action

中了解详细信息.

通过 AspectJ插件,AspectJ可以很好地集成到Maven构建中.

如果仅将AspectJ用于策略实施,则不会有任何其他运行时依赖项,因为不会修改字节码.

Basically I have packages...

com.me.application
com.me.application.thing
com.me.library

I want to enforce the rule that nothing in com.me.application can include com.me.library, except things in com.me.application.thing.

Is this possible at the level of Java code, Maven, classpath, or any other layer of Java that would be roughly equivalent to linking in C?

解决方案

The only way that I can think of is through AspectJ.

AspectJ is an aspect oriented language, that is used to add cross-cutting concerns into your application through a separate compile process. One of the classical uses of AspectJ is policy enforcement, which fits your scenario.

Basically you declare rules that decide from which package which code may be called and throw a compile error whenever you encounter a method call (or in your case a variable declaration) that violates these rules. You can learn the details in the excellent book AspectJ in Action

AspectJ can nicely be integrated into a Maven build through the AspectJ plugin

And if you use AspectJ only for policy enforcement, you won't have any additional runtime dependencies, as your byte code won't be modified.

这篇关于可以在Java中强制执行程序包依赖关系层次结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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