Spring注解开发

打印 上一主题 下一主题

主题 1047|帖子 1047|积分 3141

1、使用注解需要导入的依赖


  • 1、1在application.xml文件中加入该约束
  1. xmlns:context=http://www.springframework.org/schema/context
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.        http://www.springframework.org/schema/beans/spring-beans.xsd
  8.        http://www.springframework.org/schema/context
  9.        http://www.springframework.org/schema/context/spring-context.xsd">
  10.     <context:annotation-config/>
  11. </beans><?xml version="1.0" encoding="UTF-8" ?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.        xmlns:context="http://www.springframework.org/schema/context"
  14.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  16.        http://www.springframework.org/schema/beans/spring-beans.xsd
  17.        http://www.springframework.org/schema/context
  18.        http://www.springframework.org/schema/context/spring-context.xsd">
  19.     <context:annotation-config/>
  20. </beans>http://www.springframework.org/schema/context
  21. <?xml version="1.0" encoding="UTF-8" ?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.        xmlns:context="http://www.springframework.org/schema/context"
  24.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  26.        http://www.springframework.org/schema/beans/spring-beans.xsd
  27.        http://www.springframework.org/schema/context
  28.        http://www.springframework.org/schema/context/spring-context.xsd">
  29.     <context:annotation-config/>
  30. </beans><?xml version="1.0" encoding="UTF-8" ?>
  31. <beans xmlns="http://www.springframework.org/schema/beans"
  32.        xmlns:context="http://www.springframework.org/schema/context"
  33.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  35.        http://www.springframework.org/schema/beans/spring-beans.xsd
  36.        http://www.springframework.org/schema/context
  37.        http://www.springframework.org/schema/context/spring-context.xsd">
  38.     <context:annotation-config/>
  39. </beans>http://www.springframework.org/schema/context/spring-context.xsd
复制代码
 并且需要加入标签开启该注解
  1. <context:annotation-config/>
  2. 或指定要扫描的包,包下的注解就会生效
  3. <context:component-scan base-package="com.kuang.pojo"/>
复制代码
 最终xml代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd">
  9.     <context:annotation-config/>
  10. </beans>
复制代码

  • 1、2对应注解含义
  1. @Autowired 自动装配,优先匹配类型,后名字@Qualifier(value = "xxx")可指定自动装配的id@Resource(name = "xxx") 自动装配,优先名字,后类型,也可指定名称@Nullable 该注解后的参数可为空@Component 组件,放在类上,说明该类被spring管理了,就是bean<?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd">
  9.     <context:annotation-config/>
  10. </beans>mvc三层架构:<?xml version="1.0" encoding="UTF-8" ?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  15.        http://www.springframework.org/schema/beans/spring-beans.xsd
  16.        http://www.springframework.org/schema/context
  17.        http://www.springframework.org/schema/context/spring-context.xsd">
  18.     <context:annotation-config/>
  19. </beans><?xml version="1.0" encoding="UTF-8" ?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:context="http://www.springframework.org/schema/context"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  24.        http://www.springframework.org/schema/beans/spring-beans.xsd
  25.        http://www.springframework.org/schema/context
  26.        http://www.springframework.org/schema/context/spring-context.xsd">
  27.     <context:annotation-config/>
  28. </beans>dao:@Repository<?xml version="1.0" encoding="UTF-8" ?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:context="http://www.springframework.org/schema/context"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  33.        http://www.springframework.org/schema/beans/spring-beans.xsd
  34.        http://www.springframework.org/schema/context
  35.        http://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans><?xml version="1.0" encoding="UTF-8" ?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:context="http://www.springframework.org/schema/context"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.        http://www.springframework.org/schema/beans/spring-beans.xsd
  43.        http://www.springframework.org/schema/context
  44.        http://www.springframework.org/schema/context/spring-context.xsd">
  45.     <context:annotation-config/>
  46. </beans>service:@Service<?xml version="1.0" encoding="UTF-8" ?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:context="http://www.springframework.org/schema/context"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  51.        http://www.springframework.org/schema/beans/spring-beans.xsd
  52.        http://www.springframework.org/schema/context
  53.        http://www.springframework.org/schema/context/spring-context.xsd">
  54.     <context:annotation-config/>
  55. </beans><?xml version="1.0" encoding="UTF-8" ?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:context="http://www.springframework.org/schema/context"
  58.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  60.        http://www.springframework.org/schema/beans/spring-beans.xsd
  61.        http://www.springframework.org/schema/context
  62.        http://www.springframework.org/schema/context/spring-context.xsd">
  63.     <context:annotation-config/>
  64. </beans>Controller:@Controller功能一样,都是将该类注册到spring中,装配bean该注解需配合扫包进行使用任需ClassPathXmlApplicationContext,无法脱离配置文件@Value("xxx")该注解用于给属性进行注入,也能够直接注入与set方法中@Scope("xxx")该注解指定对应类的作用域,是单例模式或原型模式或其它lombok包下的快速生成实体类对象的注解{<?xml version="1.0" encoding="UTF-8" ?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:context="http://www.springframework.org/schema/context"
  67.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  69.        http://www.springframework.org/schema/beans/spring-beans.xsd
  70.        http://www.springframework.org/schema/context
  71.        http://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans>@NoArgsConstructor快速创建无参构造<?xml version="1.0" encoding="UTF-8" ?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:context="http://www.springframework.org/schema/context"
  76.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  77.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.        http://www.springframework.org/schema/beans/spring-beans.xsd
  79.        http://www.springframework.org/schema/context
  80.        http://www.springframework.org/schema/context/spring-context.xsd">
  81.     <context:annotation-config/>
  82. </beans>@AllArgsConstructor 快速创建有参构造<?xml version="1.0" encoding="UTF-8" ?>
  83. <beans xmlns="http://www.springframework.org/schema/beans"
  84.        xmlns:context="http://www.springframework.org/schema/context"
  85.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  86.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  87.        http://www.springframework.org/schema/beans/spring-beans.xsd
  88.        http://www.springframework.org/schema/context
  89.        http://www.springframework.org/schema/context/spring-context.xsd">
  90.     <context:annotation-config/>
  91. </beans>@Data 快速创建get,set}
复制代码
  spring4之后要使用注解必须导入aop包,若发现注解无效,可查看是否导入该包
使用java配置spring,完全舍弃spring的xml配置文件
  1. @Configuration:将类指定为spring配置类
  2. @Bean:指定该方法为xml中相当于<bean> 需返回一个实体类
  3. @ComponentScan("xxxx"):使该配置类只扫描到指定的包下
  4. @Import({xxx.class}):合并多个配置类
复制代码
SpingMVC注解开发
  1. @RequestMapping("/xxx"):该注解可映射一个访问路径,在单个方法上时直接访问 http://localhost:8080/xxx                                                在类上时访问需加上类的访问路径 http://localhost:8080/类上的映射名/xxx在返回单纯的数据时,它可以进行乱码解析<?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd">
  9.     <context:annotation-config/>
  10. </beans>@RequestMapping(value = "/sda",produces = "application/json;charset=utf-8")
复制代码
RestFul风格
  1. @PathVariable
  2. 加在参数前,可定义为路径变量
复制代码
未使用前
  1. package com.kuang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class RestfulTest {<?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd">
  9.     <context:annotation-config/>
  10. </beans>@RequestMapping("restful")<?xml version="1.0" encoding="UTF-8" ?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  15.        http://www.springframework.org/schema/beans/spring-beans.xsd
  16.        http://www.springframework.org/schema/context
  17.        http://www.springframework.org/schema/context/spring-context.xsd">
  18.     <context:annotation-config/>
  19. </beans>public String restful(int a, int b, Model model){<?xml version="1.0" encoding="UTF-8" ?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:context="http://www.springframework.org/schema/context"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  24.        http://www.springframework.org/schema/beans/spring-beans.xsd
  25.        http://www.springframework.org/schema/context
  26.        http://www.springframework.org/schema/context/spring-context.xsd">
  27.     <context:annotation-config/>
  28. </beans><?xml version="1.0" encoding="UTF-8" ?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:context="http://www.springframework.org/schema/context"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  33.        http://www.springframework.org/schema/beans/spring-beans.xsd
  34.        http://www.springframework.org/schema/context
  35.        http://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>int c = a+b;<?xml version="1.0" encoding="UTF-8" ?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:context="http://www.springframework.org/schema/context"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.        http://www.springframework.org/schema/beans/spring-beans.xsd
  43.        http://www.springframework.org/schema/context
  44.        http://www.springframework.org/schema/context/spring-context.xsd">
  45.     <context:annotation-config/>
  46. </beans><?xml version="1.0" encoding="UTF-8" ?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:context="http://www.springframework.org/schema/context"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  51.        http://www.springframework.org/schema/beans/spring-beans.xsd
  52.        http://www.springframework.org/schema/context
  53.        http://www.springframework.org/schema/context/spring-context.xsd">
  54.     <context:annotation-config/>
  55. </beans>model.addAttribute("msg",c);<?xml version="1.0" encoding="UTF-8" ?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:context="http://www.springframework.org/schema/context"
  58.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  60.        http://www.springframework.org/schema/beans/spring-beans.xsd
  61.        http://www.springframework.org/schema/context
  62.        http://www.springframework.org/schema/context/spring-context.xsd">
  63.     <context:annotation-config/>
  64. </beans><?xml version="1.0" encoding="UTF-8" ?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:context="http://www.springframework.org/schema/context"
  67.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  69.        http://www.springframework.org/schema/beans/spring-beans.xsd
  70.        http://www.springframework.org/schema/context
  71.        http://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans>return "hello";<?xml version="1.0" encoding="UTF-8" ?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:context="http://www.springframework.org/schema/context"
  76.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  77.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.        http://www.springframework.org/schema/beans/spring-beans.xsd
  79.        http://www.springframework.org/schema/context
  80.        http://www.springframework.org/schema/context/spring-context.xsd">
  81.     <context:annotation-config/>
  82. </beans>}}
复制代码

使用后
  1. package com.kuang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class RestfulTest {<?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd">
  9.     <context:annotation-config/>
  10. </beans>@RequestMapping("/restful/{a}/{b}")<?xml version="1.0" encoding="UTF-8" ?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  15.        http://www.springframework.org/schema/beans/spring-beans.xsd
  16.        http://www.springframework.org/schema/context
  17.        http://www.springframework.org/schema/context/spring-context.xsd">
  18.     <context:annotation-config/>
  19. </beans>public String restful(@PathVariable int a, @PathVariable int b, Model model){<?xml version="1.0" encoding="UTF-8" ?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:context="http://www.springframework.org/schema/context"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  24.        http://www.springframework.org/schema/beans/spring-beans.xsd
  25.        http://www.springframework.org/schema/context
  26.        http://www.springframework.org/schema/context/spring-context.xsd">
  27.     <context:annotation-config/>
  28. </beans><?xml version="1.0" encoding="UTF-8" ?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:context="http://www.springframework.org/schema/context"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  33.        http://www.springframework.org/schema/beans/spring-beans.xsd
  34.        http://www.springframework.org/schema/context
  35.        http://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>int c = a+b;<?xml version="1.0" encoding="UTF-8" ?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:context="http://www.springframework.org/schema/context"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.        http://www.springframework.org/schema/beans/spring-beans.xsd
  43.        http://www.springframework.org/schema/context
  44.        http://www.springframework.org/schema/context/spring-context.xsd">
  45.     <context:annotation-config/>
  46. </beans><?xml version="1.0" encoding="UTF-8" ?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:context="http://www.springframework.org/schema/context"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  51.        http://www.springframework.org/schema/beans/spring-beans.xsd
  52.        http://www.springframework.org/schema/context
  53.        http://www.springframework.org/schema/context/spring-context.xsd">
  54.     <context:annotation-config/>
  55. </beans>model.addAttribute("msg",c);<?xml version="1.0" encoding="UTF-8" ?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:context="http://www.springframework.org/schema/context"
  58.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  60.        http://www.springframework.org/schema/beans/spring-beans.xsd
  61.        http://www.springframework.org/schema/context
  62.        http://www.springframework.org/schema/context/spring-context.xsd">
  63.     <context:annotation-config/>
  64. </beans><?xml version="1.0" encoding="UTF-8" ?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:context="http://www.springframework.org/schema/context"
  67.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  69.        http://www.springframework.org/schema/beans/spring-beans.xsd
  70.        http://www.springframework.org/schema/context
  71.        http://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans>return "hello";<?xml version="1.0" encoding="UTF-8" ?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:context="http://www.springframework.org/schema/context"
  76.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  77.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.        http://www.springframework.org/schema/beans/spring-beans.xsd
  79.        http://www.springframework.org/schema/context
  80.        http://www.springframework.org/schema/context/spring-context.xsd">
  81.     <context:annotation-config/>
  82. </beans>}}
复制代码

restful是一种风格,并非规范或标准

restful指定访问方式

@RequestMapping(value
value可换成path,禁止使用name,会出问题
  1. package com.kuang.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class RestfulTest {<?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd">
  9.     <context:annotation-config/>
  10. </beans>@RequestMapping(value = "/restful/{a}/{b}",method = RequestMethod.GET)<?xml version="1.0" encoding="UTF-8" ?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  15.        http://www.springframework.org/schema/beans/spring-beans.xsd
  16.        http://www.springframework.org/schema/context
  17.        http://www.springframework.org/schema/context/spring-context.xsd">
  18.     <context:annotation-config/>
  19. </beans>public String restful(@PathVariable int a, @PathVariable int b, Model model){<?xml version="1.0" encoding="UTF-8" ?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:context="http://www.springframework.org/schema/context"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  24.        http://www.springframework.org/schema/beans/spring-beans.xsd
  25.        http://www.springframework.org/schema/context
  26.        http://www.springframework.org/schema/context/spring-context.xsd">
  27.     <context:annotation-config/>
  28. </beans><?xml version="1.0" encoding="UTF-8" ?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:context="http://www.springframework.org/schema/context"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  33.        http://www.springframework.org/schema/beans/spring-beans.xsd
  34.        http://www.springframework.org/schema/context
  35.        http://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>int c = a+b;<?xml version="1.0" encoding="UTF-8" ?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:context="http://www.springframework.org/schema/context"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.        http://www.springframework.org/schema/beans/spring-beans.xsd
  43.        http://www.springframework.org/schema/context
  44.        http://www.springframework.org/schema/context/spring-context.xsd">
  45.     <context:annotation-config/>
  46. </beans><?xml version="1.0" encoding="UTF-8" ?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.        xmlns:context="http://www.springframework.org/schema/context"
  49.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  50.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  51.        http://www.springframework.org/schema/beans/spring-beans.xsd
  52.        http://www.springframework.org/schema/context
  53.        http://www.springframework.org/schema/context/spring-context.xsd">
  54.     <context:annotation-config/>
  55. </beans>model.addAttribute("msg",c);<?xml version="1.0" encoding="UTF-8" ?>
  56. <beans xmlns="http://www.springframework.org/schema/beans"
  57.        xmlns:context="http://www.springframework.org/schema/context"
  58.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  60.        http://www.springframework.org/schema/beans/spring-beans.xsd
  61.        http://www.springframework.org/schema/context
  62.        http://www.springframework.org/schema/context/spring-context.xsd">
  63.     <context:annotation-config/>
  64. </beans><?xml version="1.0" encoding="UTF-8" ?>
  65. <beans xmlns="http://www.springframework.org/schema/beans"
  66.        xmlns:context="http://www.springframework.org/schema/context"
  67.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  68.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  69.        http://www.springframework.org/schema/beans/spring-beans.xsd
  70.        http://www.springframework.org/schema/context
  71.        http://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans>return "hello";<?xml version="1.0" encoding="UTF-8" ?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.        xmlns:context="http://www.springframework.org/schema/context"
  76.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  77.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.        http://www.springframework.org/schema/beans/spring-beans.xsd
  79.        http://www.springframework.org/schema/context
  80.        http://www.springframework.org/schema/context/spring-context.xsd">
  81.     <context:annotation-config/>
  82. </beans>}}
复制代码
通过在注解中选择method可以指定通过什么方式来进行访问该路径才能得到对应的方法。
通过另外的注解也能实现对应的效果
  1. @RequestMapping(name = "/restful/{a}/{b}",method = RequestMethod.GET)
  2. //get方法可以用
  3. @GetMapping("xxx")
  4. //相同的,也有DeleteMapping等对应的注解可以实现method = RequestMethod.xxx
复制代码
使用GetMapping注解接收前端参数,可直接从参数中获取,也可使用注解指定参数名
  1. @GetMapping("/t1")public ModelAndView he(@RequestParam("hs")String hs,User user){<?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:context="http://www.springframework.org/schema/context"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd">
  9.     <context:annotation-config/>
  10. </beans>System.out.println(user);<?xml version="1.0" encoding="UTF-8" ?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.        xmlns:context="http://www.springframework.org/schema/context"
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  15.        http://www.springframework.org/schema/beans/spring-beans.xsd
  16.        http://www.springframework.org/schema/context
  17.        http://www.springframework.org/schema/context/spring-context.xsd">
  18.     <context:annotation-config/>
  19. </beans>ModelAndView modelAndView = new ModelAndView();<?xml version="1.0" encoding="UTF-8" ?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.        xmlns:context="http://www.springframework.org/schema/context"
  22.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  24.        http://www.springframework.org/schema/beans/spring-beans.xsd
  25.        http://www.springframework.org/schema/context
  26.        http://www.springframework.org/schema/context/spring-context.xsd">
  27.     <context:annotation-config/>
  28. </beans>modelAndView.addObject("msg",user+"\n"+hs);<?xml version="1.0" encoding="UTF-8" ?>
  29. <beans xmlns="http://www.springframework.org/schema/beans"
  30.        xmlns:context="http://www.springframework.org/schema/context"
  31.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  33.        http://www.springframework.org/schema/beans/spring-beans.xsd
  34.        http://www.springframework.org/schema/context
  35.        http://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>modelAndView.setViewName("hello");<?xml version="1.0" encoding="UTF-8" ?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.        xmlns:context="http://www.springframework.org/schema/context"
  40.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.        http://www.springframework.org/schema/beans/spring-beans.xsd
  43.        http://www.springframework.org/schema/context
  44.        http://www.springframework.org/schema/context/spring-context.xsd">
  45.     <context:annotation-config/>
  46. </beans>return modelAndView;}
复制代码
@RequestParam("xxx") 指定该参数接收时的参数名必须为xxx
@Param("xxx")也可给指定参数一个别名
向前端返回数据,绕过视图解析器

在方法上写上@ResponseBody添加该注解,则绕过视图解析器,仅返回数据,不跳转视图
在类上添加@RestController注解,该类下的所有方法都只会返回数据,不跳转视图
Qualifier
@Qualifier

当bean中存在多个BookService类型对象时,搭配@Qualifier(“实现类名称”)表明注入的是哪一个具体实现类的bean(与                @Autowired配合使用加以区分)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

去皮卡多

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表