@Aspect类和@ControllerAdvice类的正确包层次结构是什么?

huangapple go评论73阅读模式
英文:

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
...

huangapple
  • 本文由 发表于 2023年5月29日 22:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定