SpringBoot2常用注解

@SpringBootApplication主程序类

项目引导类(引导加载自动配置类)
包装了:
1、@SpringBootConfiguration:
包装了Spring的@Configuration有属性proxyBeanMethods

  • (Full模式)true:向容器中添加组件(对象)时会检验容器中是否存在该类对象,若存在则不会创建;
  • (Lite模式)false:则不会检查 轻量级(项目初始化都肯定没有对象,所以一般都为false)

2、@EnableAutoConfiguration:初始化自动配置类

xxxxxAutoConfiguration —> 组件 —> xxxxProperties里面拿值 ----> application.properties

  • SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration(127个)
  • 每个配置类会去容器中查找想要配置的组件是否存在
  • 如果存在就根据application.properties属性配置文件设置组件属性(没有设置的属性都会有默认值)
  • 查看属性配置情况
    application.properties属性配置文件中debug=true开启自动配置报告。Negative(不生效)\Positive(生效)

3、@ComponentScan:默认只扫描与该主程序类同包及以下的包中的类


@Configuration配置类声明(作为组件加入IOC)

@Import({User.class})

这两个注释常放在配置类中

@Import({User.class})#给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
@Configuration(proxyBeanMethods = false) #告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {

	@Bean
	创建返回对象的方法......
	
}

@Conditionalxxxx

满足Conditional指定的条件才执行

@ImportResource

配置类引入原生配置文件

@ImportResource("classpath:beans.xml")
public class MyConfig {}

向IOC容器中添加组件(对象)

@Bean:配置类中
@Component:其它类中
@Controller:控制层
@Service:业务层
@Repository:数据层

@ConfigurationProperties

给对象类配置属性(properties文件中以mycar开头的配置)

@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {}

要是不加@Component也可以在配置类中:

@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
public class MyConfig {}

控制层注释

@RestController

包装了:

  • @Controller:控制层组件配置
  • @ResponseBody:就是将java类型数据转换为json数据返回给HTTP response body 中

因为都是继承HttpServlet
在这里插入图片描述
所有可以直接获得

@PostMapping("/user")
    public Object post(HttpServletRequest request, HttpServletResponse response){
        ........
    }

@PathVariable获取路径中变量

可以直接全部转换为Map集合 必须为Map<String,String>

@GetMapping("/car/{id}/{username}")
    public Map<String,Object> getCar(@PathVariable("id") Integer id,
                                     @PathVariable("username") String name,
                                     @PathVariable Map<String,String> pv){}

@RequestHeader获取请求头(也可以Map)

@RequestParam获取request中参数(也可以Map)

@RequestPart和@RequestParam类似,只是 @RequestParam取request中简单的String参数,而@RequestPart取request中复杂的类型参数(二进制、json、xml)【文件上传时使用!!】

啊啊啊!!!@RequestParam获取重定向传递的参数!!!!

@PostMapping("/registration.html")
public String register(User user,Model model,RedirectAttributes redirectAttributes){
	......
    redirectAttributes.addAttribute("tip","注册成功请登录");
    return "redirect:/login.html";
}

@GetMapping({"/","/login.html"})
public String loginPage(Model model,@RequestParam(required = false) String tip){
    if (tip != null) model.addAttribute("tip",tip);
    return "login";//跳转到login.html(不需要后缀)
}

@CookieValue 获取cookie(不能Map,但可以获得cookie对像)

@GetMapping("/car/{id}/{username}")
    public Map<String,Object> getCar(@CookieValue("_ga") String _ga,
                                     @CookieValue("_ga") Cookie cookie){}

@RequestBody获取表单提交原始数据

@PostMapping("/user")
    public String post(@RequestBody String context){
        return context;
    }
context:username=name&passwd=pass123&_method=POST

@RequestAttribute获得request中设置的属性值

注意@ResponseBoby用法:
在这里插入图片描述

@MatrixVariable

由于UrlPathHelper的setRemoveSemicolonContent功能会将路径中的分号去除,所以我们需要重写配置WebMvcConfigurer组件

//让矩阵变量可用
    @Bean
    public WebMvcConfigurer getWebMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
            各种自定义方法重写.....
        };
    }
    // /user/boos/id=1;hobby=c++,java,php
    @PutMapping("/user/{path}")
    public Map update(@MatrixVariable("id") Integer id,
                       @MatrixVariable("hobby")List<String> hobby,
                       @MatrixVariable("path") String path){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("hobby",hobby);
        map.put("path",path);
        return map;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值