英文:
What is the correct package hierarchy for @Aspect classes and @ControllerAdvice classes?
问题
Problem Details<br>
我目前正在实现Spring Boot和Spring MVC项目中的两个类。
第一个:
@ControllerAdvice
class GlobalDefaultExceptionHandler {
// 与此问题无关的部分
}
第二个:
@Aspect
@Component
public class MyAspect {
// 与此问题无关的部分
}
当前项目的包层次结构如下。
层次结构:
com.example
├── aspect
│ └── MyAspect.java
├── advice
│ └── GlobalDefaultExceptionHandler.java
Problem<br>
我不确定这是否是正确的包层次结构。
Possible Solution<br>
由于advice和aspect都是Spring AOP功能,我应该将这两个包放在一个名为'aop'的基础包中吗?
我的项目包层次结构的可能解决方案。
层次结构:
com.example
└── aop
├── aspect
│ └── MyAspect.java
│
└── advice
└── GlobalDefaultExceptionHandler.java
请纠正我,如果我错了。
英文:
Problem Details<br>
I`m currently implementing two classes in my Spring Boot, Spring MVC project.
First:
@ControllerAdvice
class GlobalDefaultExceptionHandler {
// body not relevant to this problem
}
Second:
@Aspect
@Component
public class MyAspect {
// body not relevant to this problem
}
The current project package hierarchy looks like this.
Hierarchy:
com.example
├── aspect
│ └── MyAspect.java
├── advice
│ └── GlobalDefaultExceptionHandler.java
Problem<br>
I`m not sure if this is the correct package hierarchy.
Possible Solution<br>
Since both advice and aspect are Spring AOP features, should I place these two packages in one base package called 'aop'?
A possible solution for my project package hierarchy.
Hierarchy:
com.example
└── aop
├── aspect
│ └── MyAspect.java
│
└── advice
└── GlobalDefaultExceptionHandler.java
Please, correct me if I`m wrong.
答案1
得分: 2
我建议坚持第一种方法,即根据组件类型创建包 - Repository/Controller/Service..
com.example
├── aspect
│ └── MyAspect.java
├── advice
│ └── GlobalDefaultExceptionHandler.java
首先,AOP 表示面向切面编程,与组件相关
其次,如果您查看 Spring 的 ControllerAdvice
参考文档,您将注意到它并不来自特定的 aop
相关包
import org.springframework.web.bind.annotation.ControllerAdvice;
当我个人对此类问题有疑虑时,我总是更喜欢查看 Java 库或其他库的参考文档。
英文:
I would suggest sticking to the first approach, where you have a package based on the type of components - Repository/Controller/Service..
com.example
├── aspect
│ └── MyAspect.java
├── advice
│ └── GlobalDefaultExceptionHandler.java
First, AOP means Aspect-Oriented Programming, noting component related
Second, If you check Spring ControllerAdvice
for the reference, you will notice that it doesn't come from a specific aop
related package
import org.springframework.web.bind.annotation.ControllerAdvice;
When I personally have concerns about such stuff, I always prefer to check Java libraries or some other libraries for the reference
答案2
得分: 0
根据我的观点,包的层次结构应该与其他项目保持一致,分工明确,定义清晰...
你想要多细致的层次结构?这取决于每个项目,但我会选择选项一。
我认为(在没有看到项目的其余部分的情况下),你可能会有类似这样的结构:
com.example
├── aspect
│ └── MyAspect.java
├── advice
│ └── GlobalDefaultExceptionHandler.java
├── service
│ └── MySErvice.java
├── controller
│ └── MyController.java
├── repository
│ └── MyRepository.java
...
英文:
In my opinion the package hierarchy should be consistent with the rest of the projects, with separation of responsibilities, clean definitions...
How much granularity do you want to give it? It depends on each one but I would go for option one.
I think (without seeing the rest of the project) that you will have something like this:
com.example
├── aspect
│ └── MyAspect.java
├── advice
│ └── GlobalDefaultExceptionHandler.java
├── service
│ └── MySErvice.java
├── controller
│ └── MyController.java
├── repository
│ └── MyRepository.java
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论