Spring - Spring 常用注解
其他
2019-11-25 00:34:07
阅读次数: 0
- 概述
- 简单整理一些 Spring 的注解
- 这个算是一个 水一波 类型的整理
- 内容不全
- 分类可能有的地方不会太符合逻辑
- 而且时间也不太充裕
- 先把自己想写的写下来, 然后随缘整理吧
- 约定
- 版本
- Spring
- 5
- 后悔
- 相关的书, 看了 半个月后 才想起来整理
- 现在看起来, 有点累
- 有些东西, 可能记的不那么精准
- 发现问题, 及时修补吧
1. 基本
- @SpringBootApplication
- 概述
- spring 入口类的注解
- 由 3 个老注解合成
- 类型
- 类注解
- SpringBoot 的入口类
- 组成
- @SpringBootConfiguration
- 配置类
- 是 @Configuration 的一个特殊形式
- 告诉大家, 这是一个 配置类
- @EnableAutoConfiguration
- 选项
- 开启自动配置
- 自动配置, 是 spring boot 的特性
- 用很多的约定代替了配置
- @ComponentScan
- 选项
- 开启组件扫描
- 为后面的自动注入, 做了基础
- 相关注解
- @Component
- @Controller
- @Service
- ref
- Annotation Type SpringBootApplication
- @Component
- 概述
- spring 组件注解
- 有子类
- @Controller
- @Service
- 类型
- 类注解
- 组件类
- 自动扫描
- 配合
- 需要与 @SpringBootApplication 配合
- 主要是 自动扫描
- ref
- @Component
- @Controller
- 概述
- Spring WebMvc 的注解
- 类型
- 类注解
- 出现在 Controller 上
- 配合
- 需要与 @SpringBootApplication 配合
- 主要是 自动扫描
- ref
- Annotation Type Controller
- @Service
- 概述
- Spring 注解
- 类型
- 类注解
- 出现在 Service 上
- Service 通常是某个接口的实现
- 配合
- 需要与 @SpringBootApplication 配合
- 主要是 自动扫描
- ref
- Annotation Type Service
- @Autowired
- 概述
- Spring 注解
- 自动注入
- 类型
- 对象注解
- 出现在对象声明上
- 自动识别类型进行填充
- 也可以手动执行
- 配合
- @Component
- @Controller
- @Service
- @Repository
- ref
- Annotation Type Autowired
- Spring 注解配置(2)——@Autowired
- Autowired 指定特定类型来 填充对象
- @Configuration
- 概述
- Spring 注解类
- 类型
- 类注解
- 类中会有方法
- 运行时生成 bean
- 运行时处理 对这些 bean 的秦秋
- 比如 WebConfig
- web 配置
- 本身也是 bean
- ref
- Annotation Type Configuration
- @Repository
- 概述
- Spring 数据库交互类注解
- 类型
- 类注解
- 通常会借助 各种template类, 与持久化数据交互
- ref
- Annotation Type Repository
2. mvc
- @RequestMapping
- 概述
- 声明 Controller 负责的路径
- 类型
- 类注解
- 规定了整个 controller 下的默认路径
- 下面的 处理方法, 如果没有描述 路径, 按这个路径来
- 如果描述了, 则以这个为 根路径, 继续处理
- 方法注解
- 通常还需要指定其他
- 请求方式
- 路径
- 路径中的参数
- 配合
- 各种 @Mapping
- ref
- Annotation Type RequestMapping
- 各种 @Mapping
- 概述
- 对应各种协议
- 类型
- 方法注解
- 注解处理特定协议的 controller 方法
- 注解
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping
- 配合
- @RequestMapping
- ref
- Annotation Type GetMapping
- 只给了 GetMapping
- 其他的可以从页面跳转
- @SessionAttributes
- 概述
- 声明要保存在 session 的 model 属性值
- 类型
- 类注解
- 对特定 controller 使用
- 一个 controller 使用就行
- ref
- Annotation Type SessionAttributes
- @ModelAttribute
- 概述
- model 属性
- 功能有点多
- 类型
- 属性注解
- 用在 reqeust 方法的形参里
- 可以从 请求的 model 里抓取出相应类型对象, 并传给形参
- 方法注解
- 将方法的返回值, 作为 model 对应属性的值
- 这块还需要 复习, 有点模糊了
- ref
- Annotation Type ModelAttribute
3. rest
- @RestController
- 概述
- 将 controller 标注为 restcontroller
- 类型
- 类注解
- 默认类里的 controller 方法, 带有一下注解
- @ResponseBody
- 返回 数据, 写到 responsebody 里
- ref
- Annotation Type RestController
- @PathVariable
- 概述
- 属性作为形参
- 类型
- 属性注解
- 从 url 路径中, 取出 同名字段, 作为形参
- ref
- Annotation Type PathVariable
- @ResponseStatus
- 概述
- 为 response 指定属性
- 类型
- 方法注解
- 为 response 指定属性
- 配合
- @Mapping
- @RequestMapping
- Enum HttpStatus
- ref
- Annotation Type ResponseStatus
- Enum HttpStatus
- 状态, 配合 responsestatus
- @RequestBody
- 概述
- 从 RequestBody 里获取内容
- 类型
- 属性注解
- controller 方法参数
- 从 request 的 body 里获取
- 问题, 貌似 request body 里, 必须是一个完整的对象
- ref
- Annotation Type RequestBody
4. 测试
- @RunWith(SpringRunner.class)
- 概述
- spring boot 的测试注解
- 其实是 JUnit 的一个注解
- JUnit 4
- 类型
- 类注解
- 测试类
- 需要 spring 注解, 都需要添加
- 否则 spring 相关注解会被无视
- SpringRunner.class
- 指定的 runner
- 这个不太清楚
- 一般用这个没错
- @SpringBootTest
- 概述
- spring boot 测试
- 完整的 spring 上下文
- 类注解
- 类注解
- spring boot 测试类
- 作用
- 启动 spring boot application context
- 使用默认的 SpringBootContextLoader 加载 context
- 使用默认的 @SpringBootConfiguration 加载配置
- 其他详见 api 文档吧...
- ref
- Annotation Type SpringBootTest
- @WebMvcTest
- 概述
- spring boot 测试注解
- spring web mvc 相关测试
- 类型
- 类注解
- spring boot webmvc 测试类
- 只测试 webmvc
- 补充
- 如果需要完整的 spring 上下文
- 请带上 @SpringBootTest
- 配合
- 可能会和 MockMvc 这个类做配合
- ref
- [Annotation Type WebMvcTest](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.html
- @RequestParam
- 略
ps
- ref
- spring in action 5th edition
- 其他注解
- lombok
- testng
- spring security
- 这个要补充...
- spring valid
- 这个随缘补充
- spring configuration properties
- 这个需要补充
转载自www.cnblogs.com/xy14/p/11925245.html