spring boot 注解

@SpringBootApplication:

包含@Configuration. @EnableAutoConfiguration, @ComponentScan,通常用于朱主类

@Repository:

用于标注数据访问组件,即DAO组件

@Service

用于标注业务层组件

@RestController

用于标注控制层组件,包含@Controller和@ResponseBody

@ResponseBody

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

@Component

泛指组件,当组件不好归类是,使用该注解

@ComponentScan

组件扫描,相当与<context:component-scan>, 如果扫描到有@Component @Controller @Service等这些注解的类,则把这些类注册为bean

@ Configuration

指出该类是Bean配置的信息源,相当于xml中的<beans></beans>,一般加载主类上

@Bean:

相当于xml中的<beans></beans> ,放在方法上面,而不是类。意思是产生一个bean,并且交给spring管理

@EnableAutoConfiguration

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

@AutoWired

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

@Qualifier:

当有多个同一类型的bean是,可以用@Qualifier(“name”),来制定。与@AutoWired配合使用。

@Resource(name=”name”, type=”type”)

没有括号内内容的话,默认byName.与@AutoWired干类似的事情

@RequestMapping:

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

@RequestParam

用在方法的参数前面,@RequestParam String a = request.getParameter("a");

@PathVariable:

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

  1.  
    @RequestMapping("user/get/mac/{macAddress}")
  2.  
    public String getByMacAddress(@PathVariable String macAddress){
  3.  
    //do something
  4.  
    }
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

@Profiles

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

  1.  
    @Configuration
  2.  
    @Profile("prod")
  3.  
    public class ProductionConfiguration{
  4.  
    //。。。
  5.  
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

@ConfigurationProperties

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

  1.  
    @Component
  2.  
    @ConfigurationProperties(prefix="connection")
  3.  
    public class ConnectionSettings{
  4.  
    @NotNull
  5.  
    private InetAddress remoteAddress;
  6.  
    //。。。
  7.  
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@ControllerAdvice

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

@ExceptionHandler(Exception.class)

用在方法上面表示遇到这个异常就执行如下方法

原文地址:https://www.cnblogs.com/hello-liyb/p/9324664.html