文章目录
- Spring Boot 常用注解详解
- 1. Spring Boot 核心注解
- 1.1 `@SpringBootApplication`
- 1.2 `@Configuration`
- 1.3 `@ComponentScan`
- 2. Bean 相关注解
- 2.1 `@Bean`
- 2.2 `@Component`
- 2.3 `@Service`
- 2.4 `@Repository`
- 2.5 `@Controller` 和 `@RestController`
- 3. 依赖注入注解
- 3.1 `@Autowired`
- 3.2 `@Qualifier`
- 3.3 `@Value`
- 4. Web 相关注解
- 4.1 `@RequestMapping`
- 4.2 `@GetMapping`、`@PostMapping`、`@PutMapping`、`@DeleteMapping`
- 4.3 `@PathVariable`
- 4.4 `@RequestParam`
- 5. 配置相关注解
- 5.1 `@ConfigurationProperties`
- 5.2 `@PropertySource`
- 6. 事务管理注解
- 6.1 `@Transactional`
- 7. 缓存相关注解
- 7.1 `@Cacheable`
- 7.2 `@CacheEvict`
- 8. 总结
Spring Boot 常用注解详解
Spring Boot 是一个基于 Spring 框架的快速开发框架,它通过自动配置和约定优于配置的原则,极大地简化了 Spring 应用的开发。在 Spring Boot 中,注解(Annotation)是核心组成部分之一,它们帮助我们定义组件、配置应用、处理请求等。本文将详细介绍 Spring Boot 中常用的注解及其使用场景。
1. Spring Boot 核心注解
1.1 @SpringBootApplication
@SpringBootApplication
是 Spring Boot 应用的入口注解,通常用于主类上。它是一个组合注解,包含了以下三个注解的功能:
@Configuration
:表示该类是一个配置类。@EnableAutoConfiguration
:启用 Spring Boot 的自动配置机制。@ComponentScan
:自动扫描当前包及其子包下的组件。
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
1.2 @Configuration
@Configuration
用于标记一个类为配置类,通常与 @Bean
注解一起使用,用于定义 Spring 容器中的 Bean。
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
1.3 @ComponentScan
@ComponentScan
用于自动扫描指定包下的组件(如 @Component
、@Service
、@Repository
等),并将它们注册为 Spring Bean。
@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class MyApplication {
// ...
}
2. Bean 相关注解
2.1 @Bean
@Bean
用于方法上,表示该方法返回的对象应注册为 Spring 容器中的 Bean。
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
2.2 @Component
@Component
是一个通用的注解,用于标记一个类为 Spring 组件。
@Component
public class MyComponent {
// ...
}
2.3 @Service
@Service
用于标记服务层的组件,通常用于业务逻辑层。
@Service
public class MyService {
// ...
}
2.4 @Repository
@Repository
用于标记数据访问层的组件,通常用于数据库操作。
@Repository
public class MyRepository {
// ...
}
2.5 @Controller
和 @RestController
@Controller
用于标记控制层的组件,通常用于 MVC 模式中的控制器。@RestController
是@Controller
和@ResponseBody
的组合,用于 RESTful Web 服务。
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
3. 依赖注入注解
3.1 @Autowired
@Autowired
用于自动注入依赖的 Bean。
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
}
3.2 @Qualifier
当有多个相同类型的 Bean 时,@Qualifier
用于指定具体的 Bean。
@Service
public class MyService {
@Autowired
@Qualifier("myBean1")
private MyBean myBean;
}
3.3 @Value
@Value
用于注入属性值,通常从配置文件中读取。
@Service
public class MyService {
@Value("${app.name}")
private String appName;
}
4. Web 相关注解
4.1 @RequestMapping
@RequestMapping
用于映射 HTTP 请求到处理方法,可以指定 URL 路径和 HTTP 方法。
@RestController
public class MyController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "Hello, World!";
}
}
4.2 @GetMapping
、@PostMapping
、@PutMapping
、@DeleteMapping
这些注解是 @RequestMapping
的简化版,分别用于处理 GET、POST、PUT 和 DELETE 请求。
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
4.3 @PathVariable
@PathVariable
用于将 URL 中的变量绑定到方法参数。
@GetMapping("/user/{id}")
public String getUser(@PathVariable Long id) {
return "User ID: " + id;
}
4.4 @RequestParam
@RequestParam
用于将请求参数绑定到方法参数。
@GetMapping("/user")
public String getUser(@RequestParam String name) {
return "User Name: " + name;
}
5. 配置相关注解
5.1 @ConfigurationProperties
@ConfigurationProperties
用于将配置文件中的属性绑定到 Java 对象。
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String version;
// getters and setters
}
5.2 @PropertySource
@PropertySource
用于指定外部属性文件的位置。
@Configuration
@PropertySource("classpath:app.properties")
public class AppConfig {
// ...
}
6. 事务管理注解
6.1 @Transactional
@Transactional
用于标记方法或类需要事务管理。
@Service
public class MyService {
@Transactional
public void performTransaction() {
// ...
}
}
7. 缓存相关注解
7.1 @Cacheable
@Cacheable
用于缓存方法的返回值。
@Cacheable("users")
public User getUser(Long id) {
// ...
}
7.2 @CacheEvict
@CacheEvict
用于清除缓存。
@CacheEvict(value = "users", allEntries = true)
public void clearCache() {
// ...
}
8. 总结
Spring Boot 中的注解极大地简化了开发流程,使得开发者能够更专注于业务逻辑的实现。通过本文的介绍,相信你已经对 Spring Boot 中的常用注解有了更深入的了解。在实际开发中,灵活运用这些注解,可以显著提高代码的可读性和可维护性。
希望本文对你学习 Spring Boot 有所帮助!如果你有任何问题或建议,欢迎留言讨论。