spring注解
Spring的20+种常用注解
spring4
RestController
@Controller @ResponseBody 两个注解可以替换成 @RestControllerRequestMapping可以同时配置两个请求路径:
@RequestMapping(value = {"/hello","hi"})指定请求类型以及获取请求参数的另一种写法
@RestController public class Test1 { /* * http://localhost:8080/boot/hello/?id=3 * required = false 参数时候为必须;defaultValue = "0" */ @RequestMapping(value = "/hello", method = RequestMethod.POST) public String hello(@RequestParam(value = "id", required = false, defaultValue = "0") Integer myid) { return "id= " + myid; } //http://localhost:8080/boot/hello2/9 @RequestMapping(value = "/hello2/{id}", method = RequestMethod.POST) public String hello2(@PathVariable(value = "id", required = false) Integer myid) { return "id= " + myid; } }RequestParam与PathVariable
RequestParam:获取请求参数 PathVariable:获取路径变量
请求注解
@RequestMapping *******分割线******** @GetMapping @PostMapping @PutMapping @DeleteMapping RequestMapping注解与下面几种的不同就是:下面几种注解直接指定了请求类型,是组合注解, 如@GetMapping相当于@RequestMapping(method = RequestMethod.POST)
IOC
为什么用IOC
1.对象创建统一托管
2.规范的声明周期管理
3.灵活的依赖注入
4.一直的获取对象Spring IOC注入方式和场景

声明式事务
声明式事务-配置

声明式事务-使用方式

声明式事务-事务回滚

不当的try-catch可能使事务不能生效,如发生了异常但是做了try-catch,可能使一部分执行成功,一部分执行失败,违背事务初衷。