Skip to content

SpringDoc 注解

Spring 核心注解,就这几组:

1. 标记"这个东西交给 Spring 管"

注解 用在哪
@Component 通用,任何类
@Controller Controller 层
@Service Service 层
@Repository Dao 层
> 记法:后三个都是 @Component 的"马甲",功能一样,只是语义更清楚。你甚至可以把它们全当成 @Component。
#### 2. 标记"从容器里拿东西"
注解 意思
@Autowired 自动注入(最常用)
@Resource 也是注入,Java 自带的
#### 3. AOP 相关(你刚看的)
注解 意思
@Aspect 这是个切面
@Pointcut 切哪些方法
@Before / @After / @Around 什么时候切
#### 4. 配置类相关
注解 意思
@Configuration 这是个配置类
@Bean 这个方法返回的对象交给 Spring 管

用"场景"记

  • 写业务逻辑 → @Service
  • 写接口入口 → @Controller / @RestController
  • 写数据库操作 → @Repository
  • 需要一个对象帮我干活 → @Autowired
  • 想给一批方法加日志 → @Aspect + @Around

一张速查表(收藏这个就够)

交给 Spring 管:@Component @Controller @Service @Repository @Bean
从容器拿:      @Autowired @Resource
AOP:          @Aspect @Pointcut @Before @After @Around
配置:          @Configuration
Web 请求:      @RequestMapping @GetMapping @PostMapping @RequestBody @PathVariable
注解 含义
@Tag 用在controller类上,描述此controller的信息
@Operation 用在controller的方法里,描述此api的信息
@Parameter 用在controller方法里的参数上,描述参数信息
@Parameters 用在controller方法里的参数上
@Schema 用于Entity,以及Entity的属性上
@ApiResponse 用在controller方法的返回值上
@ApiResponses 用在controller方法的返回值上
@Hidden 用在各种地方,用于隐藏其api