SpringBoot的常用注解

目录

1.@SpringBootApplication(scanBasepackages="包名")

2.@RestController

3.@ConditionalOnBean(name="stringdemo")

4.@Bean

5.@ConfigurationProperties(prefix="mycar")

6.@EnableConfigurationProperties(Car.class)

7.@PathVariable("id")

8.@RequestHeader("User-Agent")

9.@RequestParam("name")

10.@CookieValue("JSESSIONID")

11.@RequestBody

12.@RequestAttribute("msg")

13.@MatrixVariable("low")


1.@SpringBootApplication(scanBasepackages="包名")

作用:放在类上,声明这是一个SpringBoot应用,该类会有一个main方法作为Boot程序的开始。拓展:1.scanBasepackages可以重新定义这个SpringBoot覆盖的范围,默认是本包及其子包
           2.该注解=>@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan

@SpringBootApplication()
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = 
                SpringApplication.run(Application.class, args);
    }
}

2.@RestController

作用:放在普通类上,声明这是一个web层的类。并且该类中所有方法返回的字符串将作为结果在浏览器上展示,而不是进行页面跳转。
拓展:1.该注解=>@Controller+@ResponseBody

@RestController/*@Controller+@ResponseBody*/
public class helloController {
    @Autowired
    car car;
    @RequestMapping("/hello")
    public String handle01(){
        return "spring boot的第一次测试";
    }
    @RequestMapping("/car")
    public car handle02(){
        return car;
    }
}

3.@ConditionalOnBean(name="stringdemo")

作用:放到方法上,如果容器中有id为stringdemo的组件,那将向容器中注入被该注解修饰的组件

    @ConditionalOnBean(name="stringdemo")
    @Bean
    public Pet pet01(){
        Pet pet = new Pet("xiaoxiao");
        return pet;
    }

4.@Bean

作用:放到方法名上,方法名就是组件的id,返回值就是组件的类型class
拓展:多次调用该方法得到的对象是一样的

    @Bean
    public User user01(){
        User user = new User(18, "zhangsan");
        return user;
    }

5.@ConfigurationProperties(prefix="mycar")

作用:放到组件类上,为类中的属性进行赋值
拓展:属性名字必须要和配置文件中的一致,如果不一致属性值是null。该类必须是容器中已经有
           的组件

@Component("car")
@ConfigurationProperties(prefix="mycar")
public class Car {
    int money;
    String brand;
}
mycar.money=10000
mycar.brand=maibahe

6.@EnableConfigurationProperties(Car.class)

作用:放到配置类上,自动给car类的属性赋值。把Car类自动注入到容器中
拓展:与@ConfigurationProperties(prefix="mycar")注解一起使用


@Configuration
@EnableConfigurationProperties(Car.class)
public class MyConfig {
}

@ConfigurationProperties(prefix="mycar")
public class Car {
    int money;
    String brand;
}

7.@PathVariable("id")

作用:放在参数里,通过特定路径形式对参数赋值
拓展://localhost:8080/test1/12/zhangsan

    @ResponseBody    
    @RequestMapping("/test1/{id}/{username}")   //localhost:8080/test1/12/zhangsan
    public Map<String,Object> test1(@PathVariable("id") int id,
                        @PathVariable("username") String username,
                        @PathVariable Map<String,String> map){
        return null;
    }

8.@RequestHeader("User-Agent")

作用:放在参数里,将请求头中的数据封装到参数里
使用://localhost:8080/test2

    @RequestMapping("/test2")   //localhost:8080/test2
    @ResponseBody
    public String test2(@RequestHeader("User-Agent") String headString,
                        @RequestHeader Map<String,String> map){
        return map.get("user-agent");
    }

9.@RequestParam("name")

作用:放在参数里,通过固定形式来封装参数
使用://localhost:8080/test3?name=xiao&hobbies=basketball&hobbies=games

 @RequestMapping("/test3") 
 @ResponseBody 
//localhost:8080/test3?name=xiao&hobbies=basketball&hobbies=games
    public String test3(@RequestParam("name") String username,
                        @RequestParam("hobbies") List<String> hobbies) {
        return username;
    }

10.@CookieValue("JSESSIONID")

作用:放在参数里,获取secession中的某个属性
使用://localhost:8080/test4

    @RequestMapping("/test4")
    @ResponseBody
    public void save16(@CookieValue("JSESSIONID") String jsessionId,
                       @CookieValue("JSESSIONID") Cookie cookie){    
    System.out.println(jsessionId);
    }

11.@RequestBody

作用:放在参数里,获取请求体数据
使用:表单提交

    @PostMapping("/test5")
    public String test5(@RequestBody String content) {
      return content;
    }
<form action="/test5" method="post">
    名称<input type="text" name="username"><br>
    <input type="submit" value="确认提交"><br>
</form>

12.@RequestAttribute("msg")

作用:放在参数中,获得request域对象中的数据
使用

@Controller
public class Controller_demo1 {
    @RequestMapping("/goto")
    public String goTo(HttpServletRequest req){
        req.setAttribute("msg","欢迎");
        return "forward:/success";
    }
    @ResponseBody
    @RequestMapping("/success")
    public String success(@RequestAttribute("msg") String msg,
                          HttpServletRequest request){
        Object msg1 = request.getAttribute("msg");
        return msg;
    }
}

13.@MatrixVariable("low")

作用:放在参数里,获取矩阵变量的值
使用://localhost:8080/car/sell;low=24;brand=byd,audi,yd

    //开放矩阵变量的路径识别
    @Configuration
    public class MyConfig implements WebMvcConfigurer {
        @Override
        public  void configurePathMatch(PathMatchConfigurer configurer) {
            UrlPathHelper urlPathHelper=new UrlPathHelper();
            //设计为不移除;后面的内容,这样矩阵变量就能实现
            urlPathHelper.setRemoveSemicolonContent(false);
            configurer.setUrlPathHelper(urlPathHelper);
        }

    }

    @ResponseBody
    @RequestMapping("/car/{path}") //localhost:8080/car/sell;low=24;brand=byd,audi,yd
    public Map<String,Object> test1(@MatrixVariable("low") Integer low,
                                    @MatrixVariable("brand") List<String> brands,
                                    @PathVariable("path") String path){
        Map<String,Object> map=new HashMap<>();
        map.put("low",low);
        map.put("brand",brands);
        map.put("path",path);
        return map;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值