文章目录
- 一、Spring基础注解
- 二、Spring MVC核心注解
- 三、Spring Boot特色注解
- 1. 应用启动相关
- 2. Web开发增强
- 3. 高级配置
- 四、使用建议
一、Spring基础注解
| 注解 | 说明 |
|---|---|
| @Component等 Stereotype | 标识不同层次的Bean(@Controller/@Service/@Repository) |
| @Autowired | 基于类型自动装配依赖 |
| @Qualifier | 配合@Autowired按名称精确注入 |
| @Scope | 定义Bean作用域(singleton/prototype等) |
| @Configuration | 标记配置类,替代XML配置 |
| @ComponentScan | 指定组件扫描包路径 |
| @Bean | 在配置类中声明Bean |
| @Import | 导入其他配置类 |
| AOP相关注解 | @Aspect/@Before/@After等实现面向切面编程 |
二、Spring MVC核心注解
| 注解 | 说明 |
|---|---|
| @RequestMapping | 映射请求路径,可作用于类/方法,类级定义父路径 |
| @RequestBody | 解析HTTP请求体JSON数据并转换为Java对象 |
| @RequestParam | 指定查询参数名称 |
| @PathVariable | 提取URL模板变量(如/user/{id}) |
| @ResponseBody | 将返回对象序列化为JSON响应 |
| @RequestHeader | 获取指定HTTP请求头 |
| @RestController | 组合@Controller + @ResponseBody,简化REST接口开发 |
三、Spring Boot特色注解
1. 应用启动相关
| 注解 | 说明 |
|---|---|
| @SpringBootApplication | 组合注解:@SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan |
| @EnableAutoConfiguration | 开启自动配置机制,可排除特定配置 |
2. Web开发增强
| 注解 | 说明 |
|---|---|
| @RestController | 同Spring MVC,但默认启用消息转换器 |
| @GetMapping等 | 简化版@RequestMapping(@PostMapping/@PutMapping等) |
3. 高级配置
| 注解 | 说明 |
|---|---|
| @Conditional | 条件化配置Bean(如JVM参数检测) |
| @PropertySource | 加载外部属性文件 |
| @Value | 注入配置属性值 |
四、使用建议
- 分层注解:坚持MVC分层原则,@Controller处理请求、@Service封装业务逻辑
- 自动装配:优先使用构造器注入加粗样式代替字段注入提升测试性
- 配置优先级:application.properties > 配置类 > 自动配置