Java calling methods from restricted layer

2019-02-20 19:19发布

问题:

I'm not even sure if this is doable but I want to ask the community for their general opinion.

I have to solve a following problem:

My Project has 2 Layers, a Core layer and a Business layer. This Core layer is providing the Business layer. At this moment Business classes can import Core classes and do whatever they want. Core classes can only import core classes and use their methods, the access to Business classes and their respective methods is forbidden.

I want to add a specific feature: I don't want to give Core any normal access to the Business layer, but I want to create something like an interface or anything similar (I don't know how to express myself here cause I have no idea if its possible), that provides the Core classes some specific methods from Business classes to use.

Can anyone help me out, or at least give me some guidelines what to read, or where to search.

Thanks in advance for any support.

Edit: I use Spring Framework with Seam and JBoss

回答1:

What you are probably facing is the design of module dependency and circular dependency. In short, the Business methods that Core needs may have to be re-factored into a 'utils' module or a 'Business-Core' module that is then accessible by both. I am only guessing here.

In addition to automating builds, a build tool like Maven also encourages such modularization and dependency. It is not unusual for a project that started off as one module to be split into 3-4 modules by the time it is done.



回答2:

This can be achieved by using AspectJ. You can write rules in AspectJ-syntax which will give build-time-errors if not enforced. See the article Archtectual Enforcement with Aid of AspectJ for an example.