zoukankan      html  css  js  c++  java
  • springboot常用注解

    Springboot 常用注解

    @SpringBootApplication:

    包含 @Configuration、@EnableAutoConfiguration、@ComponentScan
    通常用在主类上。

    @Repository:
    用于标注数据访问组件,即 DAO 组件。

    @Service:
    用于标注业务层组件。

    @RestController:
    用于标注控制层组件(如 struts 中的 action),包含 @Controller 和 @ResponseBody。

    @ResponseBody:
    表示该方法的返回结果直接写入 HTTP response body 中
    一般在异步获取数据时使用,在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @responsebody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。比如异步获取 json 数据,加上 @responsebody 后,会直接返回 json 数据。

    @Component:
    泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

    @ComponentScan:
    组件扫描。个人理解相当于,如果扫描到有 @Component @Controller @Service 等这些注解的类,则把这些类注册为 bean。

    @Configuration:
    指出该类是 Bean 配置的信息源,相当于 XML 中的,一般加在主类上。

    @Bean:
    相当于 XML 中的,放在方法的上面,而不是类,意思是产生一个 bean, 并交给 spring 管理。

    @EnableAutoConfiguration:
    让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,一般加在主类上。

    @AutoWired:
    byType 方式。把配置好的 Bean 拿来用,完成属性、方法的组装,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
    当加上(required=false)时,就算找不到 bean 也不报错。

    @Qualifier:
    当有多个同一类型的 Bean 时,可以用 @Qualifier(“name”)来指定。与 @Autowired 配合使用

    @Resource(name=“name”,type=“type”):
    没有括号内内容的话,默认 byName。与 @Autowired 干类似的事。

    @RequestMapping:

    RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
    该注解有六个属性:
    params:指定 request 中必须包含某些参数值是,才让该方法处理。
    headers:指定 request 中必须包含某些指定的 header 值,才能让该方法处理请求。
    value:指定请求的实际地址,指定的地址可以是 URI Template 模式
    method:指定请求的 method 类型, GET、POST、PUT、DELETE 等
    consumes:指定处理请求的提交内容类型(Content-Type),如 application/json,text/html;
    produces:指定返回的内容类型,仅当 request 请求头中的(Accept)类型中包含该指定类型才返回

    @RequestParam:
    用在方法的参数前面。
    @RequestParam String a =request.getParameter(“a”)。

    @PathVariable:

    路径变量。参数与大括号里的名字一样要相同。

    RequestMapping("user/get/mac/{macAddress}") public String getByMacAddress(@PathVariable String macAddress){//do something;
    }
    
    

    @Profiles
    Spring Profiles 提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。
    任何 @Component 或 @Configuration 都能被 @Profile 标记,从而限制加载它的时机。

    @Configuration
    @Profile(“prod”) public class ProductionConfiguration { // …
    }

    @ConfigurationProperties
    Spring Boot 将尝试校验外部的配置,默认使用 JSR-303(如果在 classpath 路径中)。
    你可以轻松的为你的 @ConfigurationProperties 类添加 JSR-303 javax.validation 约束注解:

    @Component
    @ConfigurationProperties(prefix="connection") public class ConnectionSettings {
    @NotNull private InetAddress remoteAddress; // ... getters and setters
    }
    
    
    

    全局异常处理

    @ControllerAdvice:
    包含 @Component。可以被扫描到。
    统一处理异常。

    @ExceptionHandler(Exception.class):
    用在方法上面表示遇到这个异常就执行以下方法。

  • 相关阅读:
    xgzc— math 专题训练(一)
    floyd判圈算法
    CF961G Partitions
    luoguP4778 Counting swaps
    AT3913 XOR Tree(巧妙转换+状压dp)
    手动实现aop编程
    代理模式
    spring重点一:处理对象创建时间 个数以及方式
    spring helloword
    spring用来干什么,解决的问题
  • 原文地址:https://www.cnblogs.com/wirr/p/8548853.html
Copyright © 2011-2022 走看看