Modules vs Layers in Java package structure

天大地大妈咪最大 提交于 2019-12-04 05:29:05

Modules-first.

I had a project that was initially layer-first, but it became too bulky to read and maintain, so we refactored it. It used AOP as well - without any problems. We just used .. in the middle of the package definition (we used spring aop with aspectj syntax). Here's how it looks like:

execution(* com.foo.app.modules..service..*.*(..))

This matches both modules.module1.service and modules.module2.service

Organizing by module allows developers to focus on feature sets as the unit of delivery rather than technical infrastructure. Scaling your code base is probably easier if you break things up based on modules - some open source projects whose code I have looked at (such as Artifactory and Continuum) organized things this way, but I haven't looked at enough to know if this is the general trend.

It probably does depend though, depending on your code base size.

I'll admit, I've never really done a lot of (formal) AOP.

Personally, I would put modules first.

That way, if you later split the modules into several JAR/WAR files (into separate maven project, for example), they are already in the correct directory structure to split them by module.

I would organize the package hierarchy by layer in order to allow your tools to work. Each module would go into it's own project with it's own source folder. This gives your IDE and developers easy module-orient grouping but your runtime tools easy layer oriented grouping

I rather put module first as well, but it seems to me that doing that you have to reference everything everywhere. It might be the reason for such confusion among developers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!