springboot 注解笔记
1、springboot启动类
1 @SpringBootApplication
包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。
2 @ComponentScan
让spring Boot扫描到Configuration类并把它加入到程序上下文。
3 @MapperScan (value = “com.*.mapper”)
扫描数据访问接口
4 @EnableDubboConfiguration
开启dubbo注解支持
2 控制层
1 @Controller
bean注入
2 @Reference
@Reference(interfaceClass =CategoryService.class,version = “1.0.0”)
//引入服务 springboot 整合dubbo时候 comsumer接收provide 暴露的 service接口
3 @Component
bean 注入
4 @GetMapping @PostMapping
@DeleteMapping @PutMapping
等价 @RequestMapping(value = “/user”,method = RequestMethod.POST)
value 取值最好为结果 实体类名 例如 /user
5 @RestController
包含 @Controller
@ResponseBody
6@Value
将配置文件中的值映射到一个Spring管理的Bean的属性上。
7 @ConfigurationProperties
@ConfigurationProperties(prefix=“配置文件中的key前缀”)
可以将配置文件中的配置自动与数据实体进行映射,配置项通过实体的set方法注入(数据实体必须提供set方法)
如果使用@ConfigurationProperties时出现警告,可以添加以下依赖
8@RequestMapping(“/quick”)
请求
9 @ResponseBody
请求体
10 @Autowired
自动注入
//Autowried 是spring 提供 默认bytpye ,要用byname的话 加@Qulifire(“name”)
//这个name 与 bean 申明时候的component(“name”)一致
//@requried = ture 报错 =flase 不报错null
11@PathVariable
路径传参
@PostMapping(“/user/a{uname}b/c{pwd}d”) //{uname} 可以设置通配符通配符
@PathVariable(“uname”) String username,//路径传参 @PathVariable(“pwd”) String password,
12 @RequestParam
@RequestParam(required = false) String hobby,//表单参数 required = false 可以不传参
13 RequestBody
@RequestBody User user//json数据 @RequestParam 与 @RequestBody(前端响应后端) 二选一
3 service层
1 @Transactional
开启事务
2 @Service
@Service(interfaceClass = CategoryService.class,version = “1.0.0”)
springboot整合dubbo 时候service 层暴露service接口
3 @Service
bean注入
4
4 测试类
1 @SpringBootTest
测试类
2 @Test
5 配置类
1 @Configuration
标志配置类 ,配置拦截器等
2 @Import:
用来导入其他配置类。
3 @ImportResource:
用来加载xml配置文件。
未完待续