0
点赞
收藏
分享

微信扫一扫

40 个 SpringBoot 常用注解-提高生产力

🟢 一、Spring Boot 启动类相关注解

1. @SpringBootApplication

组合注解,包含 @Configuration@EnableAutoConfiguration@ComponentScan

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

🔵 二、通用组件注册类注解(用于注入 Bean)

2. @Component

标记为组件,交由 Spring 容器管理。

@Component
public class MyService {}

3. @Service

功能等同于 @Component,语义化地用于标记 业务逻辑层 Bean。

@Service
public class OrderService {}

4. @Repository

标记为 DAO 层 Bean,支持 Spring 异常转换。

@Repository
public class UserRepository {}

5. @Controller

用于标记控制器(Spring MVC 层),处理 Web 请求。

@Controller
public class WebController {}

6. @RestController

组合注解,等同于 @Controller + @ResponseBody,直接返回 JSON。

@RestController
public class ApiController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }
}

🟡 三、依赖注入与配置相关注解

7. @Autowired

自动注入依赖对象(按类型)。

@Autowired
private OrderService orderService;

8. @Qualifier

@Autowired 搭配使用,指定注入 Bean 名称。

@Autowired
@Qualifier("orderServiceV2")
private OrderService orderService;

9. @Resource(JDK)

按名称注入(来自 JSR-250)。

@Resource(name = "orderService")
private OrderService orderService;

10. @Value

注入配置值(支持 EL 表达式)。

@Value("${app.name}")
private String appName;

11. @Configuration

声明配置类,相当于 applicationContext.xml

@Configuration
public class AppConfig {}

12. @Bean

定义一个 Bean,并添加到 Spring 容器中。

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

13. @ConfigurationProperties

批量绑定配置属性(推荐用于配置类)。

@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private String version;
    // getter / setter
}

14. @PropertySource

加载指定的配置文件。

@PropertySource("classpath:custom.properties")
public class CustomConfig {}

🟠 四、Web 层注解(Spring MVC)

15. @RequestMapping

通用请求映射(支持 GET/POST 等)。

@RequestMapping("/api")
public class ApiController {}

16. @GetMapping、17. @PostMapping、18. @PutMapping、19. @DeleteMapping

简化版 HTTP 映射注解。

@GetMapping("/user/{id}")
public User getUser(@PathVariable Long id) { ... }

20. @PathVariable

绑定 URL 路径变量。

@GetMapping("/user/{id}")
public String getUser(@PathVariable("id") Long id) {}

21. @RequestParam

绑定请求参数 (?id=123)。

@GetMapping("/search")
public String search(@RequestParam String keyword) {}

22. @RequestBody

将请求体(JSON)绑定到对象。

@PostMapping("/user")
public String create(@RequestBody User user) {}

23. @ResponseBody

将返回值序列化为 JSON(自动使用 Jackson)。

🔴 五、AOP 相关注解

24. @Aspect

定义切面类。

@Aspect
@Component
public class LogAspect {}

25. @Before、26. @After、27. @Around、28. @AfterReturning、29. @AfterThrowing

定义增强逻辑。

@Before("execution(* com.example.service.*.*(..))")
public void logBefore() {
    System.out.println("Before method call");
}

🟣 六、事务控制注解

30. @Transactional

声明式事务管理。

@Transactional
public void updateUser() {
    // 多个操作,要么全部成功,要么全部回滚
}

🟤 七、其他重要注解

31. @EnableAutoConfiguration

启用自动配置(已包含在 @SpringBootApplication 中)。

32. @ComponentScan

扫描指定包下的 Bean。

33. @EnableScheduling

开启定时任务。

34. @Scheduled

定时任务方法标记。

@Scheduled(cron = "0 0 * * * ?")
public void doTask() {}

35. @EnableAsync

开启异步任务执行。

36. @Async

将方法异步执行。

@Async
public void sendEmail(String email) {}

37. @EnableCaching

开启缓存。

38. @Cacheable

缓存方法返回值。

@Cacheable("users")
public User getUser(Long id) {}

39. @CacheEvict

清除缓存。

@CacheEvict(value = "users", key = "#id")
public void deleteUser(Long id) {}

40. @ConditionalOnProperty

根据配置是否存在来决定是否加载 Bean。

@Bean
@ConditionalOnProperty(name = "app.featureX.enabled", havingValue = "true")
public FeatureXService featureXService() {
    return new FeatureXService();
}

✅ 总结图(简略)

类别

常用注解

启动相关

@SpringBootApplication

组件定义

@Component@Service@Repository@Controller@RestController

注入配置

@Autowired@Value@Configuration@Bean

Web MVC

@RequestMapping@GetMapping@RequestParam

AOP

@Aspect@Before@Around

事务

@Transactional

扩展支持

@EnableScheduling@Async@EnableCaching

举报

相关推荐

0 条评论