目录
- 1-10 Spring常用注解解析
- 11-20 Spring进阶注解深度解析
- 21-30 Spring进阶注解深度解析
- 结语
前言:
欢迎来到探索Spring注解的奇妙之旅!在这篇博客中,我们将深入研究Spring框架中最常用的30个注解,揭示它们的作用和用法,助你更好地掌握Spring的强大功能。
Spring作为一个广泛应用的Java框架,注解是其核心特性之一,为开发人员提供了便捷、灵活的方式来配置和管理应用。无论你是初学者还是有一定经验的开发者,这篇博客都将为你解密Spring注解的奥秘,为你提供实用而有深度的注解应用技巧。
让我们一同踏上Spring注解之旅,深入了解这些神奇的注解,为你的Spring应用增色不少。祝你在这趟注解之旅中有所收获,希望这篇博客成为你学习Spring注解的得力指南! 🌼🌐
1-10 Spring常用注解解析
-
@Autowired - 自动注入
@Autowired private MyService myService;
-
@Controller - 控制器
@Controller public class MyController {// Controller code }
-
@Service - 服务层
@Service public class MyService {// Service code }
-
@Repository - 数据仓库
@Repository public class MyRepository {// Repository code }
-
@RequestMapping - 请求映射
@Controller @RequestMapping("/my") public class MyController {// Controller code }
-
@GetMapping - GET请求映射
@Controller @RequestMapping("/my") public class MyController {@GetMapping("/info")public String getInfo() {// Method code} }
-
@PostMapping - POST请求映射
@Controller @RequestMapping("/my") public class MyController {@PostMapping("/create")public String create() {// Method code} }
-
@PathVariable - 路径变量
@Controller @RequestMapping("/my") public class MyController {@GetMapping("/info/{id}")public String getInfo(@PathVariable Long id) {// Method code} }
-
@RequestParam - 请求参数
@Controller @RequestMapping("/my") public class MyController {@GetMapping("/info")public String getInfo(@RequestParam String name) {// Method code} }
-
@ResponseBody - 返回JSON格式数据
@Controller @RequestMapping("/my") public class MyController {@GetMapping("/info")@ResponseBodypublic Map<String, String> getInfo() {// Method code}
11-20 Spring进阶注解深度解析
-
@Configuration - 配置类
@Configuration public class AppConfig {// Configuration code }
-
@Bean - 定义Bean
@Configuration public class AppConfig {@Beanpublic MyService myService() {return new MyServiceImpl();} }
-
@ComponentScan - 组件扫描
@Configuration @ComponentScan("com.example") public class AppConfig {// Configuration code }
-
@Value - 注入属性值
@Component public class MyComponent {@Value("${my.property}")private String property; }
-
@Qualifier - 限定符注入
@Autowired @Qualifier("myServiceImpl") private MyService myService;
-
@Profile - 环境配置
@Configuration @Profile("dev") public class DevConfig {// Dev configuration code }
-
@Scope - Bean作用域
@Component @Scope("prototype") public class MyComponent {// Component code }
-
@Lazy - 懒加载
@Component @Lazy public class MyComponent {// Component code }
-
@Async - 异步方法
@Service public class MyService {@Asyncpublic void asyncMethod() {// Async method code} }
-
@Scheduled - 定时任务
@Service public class MyService {@Scheduled(fixedRate = 5000)public void scheduledMethod() {// Scheduled method code} }
21-30 Spring进阶注解深度解析
-
@Transaction - 事务管理
@Service public class MyService {@Transactionalpublic void performTransaction() {// Transactional method code} }
-
@Cacheable - 缓存数据
@Service public class MyService {@Cacheable("myCache")public String getCachedData() {// Cached data retrieval code} }
-
@CacheEvict - 清除缓存
@Service public class MyService {@CacheEvict("myCache")public void clearCache() {// Cache clearing code} }
-
@Validated - 参数验证
@RestController public class MyController {@PostMapping("/validate")public ResponseEntity<String> validateData(@RequestBody @Validated MyDto myDto) {// Validation code} }
-
@ExceptionHandler - 异常处理
@ControllerAdvice public class GlobalExceptionHandler {@ExceptionHandler(MyException.class)public ResponseEntity<String> handleMyException(MyException ex) {// Exception handling code} }
-
@ModelAttribute - 模型属性
@Controller public class MyController {@ModelAttribute("myData")public MyData prepareData() {// Model attribute preparation code} }
-
@CookieValue - 获取Cookie值
@Controller public class MyController {@GetMapping("/getCookie")public String getCookieValue(@CookieValue(name = "myCookie") String cookieValue) {// Cookie value retrieval code} }
-
@RequestHeader - 获取请求头
@Controller public class MyController {@GetMapping("/getHeader")public String getHeaderValue(@RequestHeader("myHeader") String headerValue) {// Header value retrieval code} }
-
@ResponseStatus - 设置响应状态
@RestController public class MyController {@ResponseStatus(HttpStatus.CREATED)@PostMapping("/createResource")public void createResource() {// Resource creation code} }
-
@PropertySource - 加载属性文件
@Configuration @PropertySource("classpath:my.properties") public class MyConfig {// Configuration code }
结语
感谢你的持续关注,希望这30个Spring注解能够为你的Spring项目开发提供更多便利和灵活性。Spring注解的丰富功能,让开发变得更加高效、简洁。
期待下次与你一同探讨更多的Spring框架知识,愿你在Spring的世界中越走越远,创造出更加出色的应用! 🌺📚