• spring注解开发


    Component注解,Repository注解,Service注解,Controller注解都是用来修饰类,表示创建一个这个类的对象,并将这个对象放到spring容器里面,也就是将类注册到容器里面

    1.Componet注解:

    1. //等价于在beans.xml文件中new了一个User类对象
    2. //即等价于
    3. //或者说是User user=new User();反正就是创建了一个对象并且把它放到容器里面
    4. @Component
    5. public class User
    6. {
    7. public String name;
    8. }

     如果@Component注解后面加上一个字符串,表示给这对象起一个名字(也就是作为这个bean的名称),如果没有加这个字符串,那么这个bean的名称就是类名第一个字母小写,其他字母不变,比如这个类名是User,那么在容器里这个bean的名称就是user

    1. @Component("test")
    2. //相当于User test=new User();
    3. public class User
    4. {
    5. public String name;
    6. }

    Component 还有几个衍生注解(和mvc三层架构相对应)

    (1)dao层的习惯用:Repository

    1. @Repository
    2. public class UserDao
    3. {
    4. }

    (2)service层的,习惯用service

    1. @Service
    2. public class UserService
    3. {
    4. }

    (3)controller层(Servlet层):习惯用Controller

    1. @Controller
    2. public class UserController
    3. {
    4. }

    2.ComponetScan注解:

    会去某个目录中或者某个类中寻找我们需要的bean(也就是说指定扫描范围),然后去指定范围里面找 @Component注解,@Controller注解,@Service注解,@Dao注解等,然后创建这些注解所指向的类的对象,并加入到容器里面

    如果不设置value属性,默认扫描启动类(xxxxxApplication.java)所在的目录下的所有类

    所以最好还是写这个注解,这样可以减少加载时间,提高系统启动速度

    3.Bean注解:

    用于将一个类中的方法产生的对象放到spring容器里面

    这里Bean注解可以让test方法产生的B类的对象交给Spring容器管理

    1. public A
    2. {
    3. @Bean
    4. public B test()
    5. {
    6. return new B();
    7. }
    8. }

    4.Autowired注解   当一个属性是对象属性的时候,给对象属性赋值 

    默认采用byType按类型进行赋值(进行装配)

    常见使用场景:成员变量是一个类的对象

    1. public class Test
    2. {
    3. @Autowired
    4. private User user;
    5. }

    再加一个Qualifier注解,两个注解组合使用,就可以实现ByName装配

    1. public class Test
    2. {
    3. @Autowired
    4. @Qualifier("user1")
    5. private User user;
    6. }

    5.@Resource注解  也是给对象属性赋值

    默认采用byName进行装配,byName找不到就byType

    1. public class Test
    2. {
    3. @Resource("user1")
    4. private User user;
    5. }

    6.Value注解  设置默认值

    1. @Value("ouyangshuiming")
    2. public String name;

    7.@ResponseBody注解表示针对某个Http请求,返回的是一个字符串,而不是一个html页面

    如果是这样,看起来返回的是字符串,其实返回的并不是字符串,而是会跳转到hello.html页面,也就是说返回的其实是一个页面,而不是一个字符串

    1. @Controller
    2. public class UserController
    3. {
    4. @RequestMapping(“/login”)
    5. public String login()
    6. {
    7. return "hello";
    8. }
    9. }

     加上@ResponseBody注解之后,返回的就真的是字符串而不是html页面了

    1. @Controller
    2. @ResponseBody
    3. public class UserController
    4. {
    5. @RequestMapping(“/login”)
    6. public String login()
    7. {
    8. return "hello";
    9. }
    10. }

     如果我们返回一个对象,那么就会将这个对象转换成json数据(也就是一个一个的键值对,key1:value1,key2:value2,key3:value3),举例如下:

    1. public class User
    2. {
    3. private int id;
    4. private String username;
    5. private String birthday;
    6. private String sex;
    7. private String address;
    8. //添加get\set\构造方法
    9. ...
    10. }
    11. @ResponseBody
    12. @RequestMapping("/login3")
    13. public User login3()
    14. {
    15. User user = new User(1, "张三", "1990-2-1", "男", "武汉");
    16. return user;
    17. }

    浏览器访问,返回的结果如下:

    @RestController注解,这个注解相当于@Controller+@ResponseBody

    8.Scope

    标注一个类是单例模式或者原型模式(prototype):

    1. @Scope("singleton")
    2. public class User
    3. {
    4. }

    9.@Configuration   声明一个类为配置类

    @Configuration 注解可以用来替代传统的 XML 配置文件。在过去,Spring 应用程序的配置通常是通过编写 XML 文件来定义和配置 bean、依赖关系、切面、数据源等信息。而使用 @Configuration 注解可以将这些配置信息直接以 Java 代码的方式进行声明和管理,从而实现基于代码的配置,称为 Java Config。

    1. @Configuration
    2. public class AppConfig {
    3. @Bean
    4. public UserService userService() {
    5. return new UserServiceImpl();
    6. }
    7. @Bean
    8. public UserController userController(UserService userService) {
    9. return new UserController(userService);
    10. }
    11. @Bean
    12. public DataSource dataSource() {
    13. // 配置数据源
    14. DataSource dataSource = new DataSource();
    15. dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
    16. dataSource.setUsername("root");
    17. dataSource.setPassword("password");
    18. return dataSource;
    19. }
    20. @Bean
    21. public TransactionManager transactionManager(DataSource dataSource) {
    22. // 配置事务管理器
    23. return new TransactionManager(dataSource);
    24. }
    25. }

    10.@PathVariable

    表示id这个参数的值等于请求路径url里面的id

    1. @GetMapping("/{id}")
    2. public R getById(@PathVariable Long id)
    3. {
    4. }

    11.@RequestBody注解:

    http请求长什么样?

    (1)post请求  Post请求通过请求体传递参数,通过请求体传参可以分为两种类型:

    第一种:当请求的内容属于表单类型的时候,类似于get请求,也是通过key-value对的形式传参的

    1. POST Http://localhost:9009/student/add
    2. Content-Type:application/x-www-form-urlencoded
    3. name=zhangsan&age=20

    第二种:请求的内容类型是json类型 ,这个时候就是通过json串的形式进行传参的

    1. POST Http://localhost:9009/student/add
    2. Content-Type:application/json
    3. {
    4. "name":"zhangsan";
    5. "age":15;
    6. }

    第一行是url行,指定了当前请求的类型(Post)和请求的url

    第二行是一个请求头(包含了很多信息,比如请求的身份信息,还有很重要的请求内容的类型(比如json类型)

    然后是一个空行

    再然后就是请求体,也就是body的部分

    (2)get请求  get请求是通过url传参,也就是url里面问号后买你的key,value对的形式传递参数

    1. GET http://localhost:9009/student/add?name=zhangsan&age=20

    前置知识介绍完毕

    1. @Data
    2. public class Student
    3. {
    4. private String name;
    5. private int age;
    6. private int gender;
    7. }
    1. @RestController
    2. @RequestMapping("/student")
    3. public class StudentController
    4. {
    5. @RequestMapping("/add")
    6. public String save(@RequestBody Student student)
    7. {
    8. System.out.println(student);
    9. return "ok";
    10. }
    11. }

    get请求传参的时候,不能用RequestBody注解,因为get请求没有请求体

    post请求表单类型的时候,也不能使用RequestBody注解,因为请求体是键值对

    只有post请求,而且请求体是json类型的时候,才可以使用RequestBody注解

    1. POST Http://localhost:9009/student/add
    2. Content-Type:application/json
    3. {
    4. "name":"zhangsan";
    5. "age":15;
    6. }

     发现post请求中没有穿的参数就会被置为默认值

    Student(name=zhangsan,age=15,gender=0)

    其他两种请求直接这样即可:

    1. @RestController
    2. @RequestMapping("/student")
    3. public class StudentController
    4. {
    5. @RequestMapping("/add")
    6. public String save(Student student)
    7. {
    8. System.out.println(student);
    9. return "ok";
    10. }
    11. }

    总结:

    对于post请求而言:请求体是json类型,需要加上RequestBody注解,请求体是key-value类型,不需要加上RequestBody注解

    对于get请求,不需要加上RequestBody注解

    12.RequestParam注解

    比如你前端传过来的参数名叫做studentName,这个参数名和后端的属性名是不一样的(后端的属性名是name),为了解决前后端属性名不一致的问题,使用这个注解

    下面的代码意思就是将前端传过来的studentName这个参数赋值给name这个变量

    前端传过来的是studentName=“zhangsan”,将这个值赋值给save方法的name属性

    1. @RestController
    2. @RequestMapping("/student")
    3. public class StudentController
    4. {
    5. @RequestMapping("/add")
    6. public String save(@RequestParam("studentName") String name)
    7. {
    8. return "ok";
    9. }
    10. }

    13.GetMapping  PostMapping  PutMapping   DeleteMapping

    (1)GetMapping  用来处理get请求

    传统的RequestMapping  :

    1. @RequestMapping(value="/get/{id}",method=RequestMethod.GET)
    2. public void getId(@PathVariable int id)
    3. {
    4. }

    使用GetMapping:大大简化

    1. @GetMapping("/get/{id}")
    2. public void getId(@PathVariable int id)
    3. {
    4. }

    (2)PostMapping  用来处理Post请求,和GetMapping同理

    (3)PutMapping:它的使用方法与PostMapping几乎是一样的,没什么区别

    (4)DeleteMapping 删除URL映射

     

     

  • 相关阅读:
    10.1 校招 实习 内推 面经
    后端nginx报reset by peer while reading upstream
    Windows docker desktop 基于HyperV的镜像文件迁移到D盘
    【云原生 | Kubernetes 系列】K8s 实战 如何给应用注入数据 II 将pod数据传递给容器
    Iocomp ActiveX v5 SP6 带OPC -Crack
    Java—通过sign签名认证实现安全的开放接口API
    springboot集成excel导入导出
    yaml文件详解
    Windows10下Git2.37.1安装及配置完整版
    读书笔记之C Primer Plus 1
  • 原文地址:https://blog.csdn.net/weixin_47414034/article/details/126653902