【SpringBoot 2学习笔记】《十四》SpringBoot2 大话注解

SpringBoot2核心注解说明

现在企业的项目开发,Java应用开发领域目前最火的框架非SpringBoot2莫属,它可以方便地创建生产级的Spring应用程序,还能通过注解配置的方式与微服务框架SpringCloud集成。其最核心在于SpringBoot2极大地简化了项目的配置,尽其所能地实现了约定大于配置的原则。

然而要想充分利用SpringBoot2就需要对其提供的各类注解有清晰地认识和深刻的理解。所以本章主要归纳整理一些常用注解。

10.1 @SpringBootApplication

SpringBoot2核心注解,该注解使用在SpringBoot2主类上,标识这是一个SpringBoot2应用启动类入口。该注解是一个复合注解,由 @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan三个注解的组合,即可以用这三个注解来代替@SpringBootApplication注解。

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    
}

10.2 @EnableAutoConfiguration

表示开启自动配置功能,开启该注解之后,SpringBoot2自动配置注解,根据当前类路径下的包或者类来配置 Spring Bean。通过观察@EnableAutoConfiguration源码,发现它也是一个组合注解,包含以下两个注解:@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class})

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    
}

Springboot2会根据我们在pom.xml文件中添加的jar包来配置项目的默认配置。例如:通过spring-boot-starter-web来判断项目中是否需要添加了tomcat,并自动的帮我们配置web项目中所需要的默认配置。

10.3 @Configuration

Spring 3.0以后添加的一个注解,可以使用Java代码的形式代替XML配置文件(applicationContext.xml)的效果,所有这个配置文件里面能做到的事情都可以通过这个注解所在类来进行注册。使用该注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContextAnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    
}

10.4 @SpringBootConfiguration

该注解是@Configuration注解的变体,仅仅用来表示是SpringBoot2配置而已。该注解继承自@Configuration,二者功能一致。用来标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到Srping容器中,并且实例名就是方法名。

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    
}

10.5 @ComponentScan

@ComponentScan注解可以自定义Spring扫描的包,默认会扫描标包路径下面标注了@Controller@Service@Component以及@Repository注解的类,并将这些组件实例化后加载到SpringIOC容器中,它有个配置属性:basePackages,也就是指定扫描的包。如果不设置,它会默认扫描配置了该注解的类的包所在的路径(包括子包)。

注解源码:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    
}

10.6 @Conditional

Spring4.0添加的新注解。@Conditional类型的注解,可以用在@Configuration注解的类上,或者@Bean注解方法中使用。可以允许基于Spring Environment属性包含配置,可以仅允许在存在特定资源时包含配置。

注解源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
    
}

10.6.1 @ConditionalOnBean & @ConditionalOnMissingBean

@ConditionalOnBean:组合@Conditional注解,当容器中有指定的 Bean 才开启配置。

@ConditionalOnMissingBean:组合@Conditional注解,和@ConditionalOnBean注解相反,当容器中没有指定的Bean才开启配置。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnBean {
    
}

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnMissingBean {
    
}

10.6.2 @ConditionalOnClass & @ConditionalOnMissingClass

@ConditionalOnClass:组合@Conditional注解,当容器中有指定的Class才开启配置。

@ConditionalOnMissingClass:组合@Conditional注解,当容器中没有指定的Class才开启配置。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {

}

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnMissingClass {
    
}

10.6.3 @ConditionalOnWebApplication & @ConditionalOnNotWebApplication

@ConditionalOnWebApplication:组合@Conditional注解,当前项目类型是WEB项目才开启配置。

@ConditionalOnNotWebApplication:组合@Conditional注解,当前项目类型不是WEB项目才开启配置。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnWebApplication {
    
}

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnNotWebApplication {

}

10.6.4 @ConditionalOnProperty

@ConditionalOnProperty:组合@Conditional注解,当指定的属性有指定的值时才开启配置。

注解源码:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
    
}

10.6.5 @ConditionalOnExpression

@ConditionalOnExpression:组合@Conditional注解,当SpEL表达式为true时才开启配置。

注解源码:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnExpressionCondition.class)
public @interface ConditionalOnExpression {
    
}

10.6.6 @ConditionalOnJava

@ConditionalOnJava:组合@Conditional注解,当运行的JVM在指定的版本范围时才开启配置。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnJavaCondition.class)
public @interface ConditionalOnJava {
    
}

10.6.7 @ConditionalOnResource

@ConditionalOnResource:组合@Conditional注解,当类路径下有指定的资源才开启配置。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnResourceCondition.class)
public @interface ConditionalOnResource {
    
}

10.6.8 @ConditionalOnJndi

@ConditionalOnJndi:组合@Conditional注解,当指定的 JNDI 存在时才开启配置。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnJndiCondition.class)
public @interface ConditionalOnJndi {
    
}

10.6.9 @ConditionalOnCloudPlatform

@ConditionalOnCloudPlatform:组合@Conditional注解,当指定的云平台激活时才开启配置。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnCloudPlatformCondition.class)
public @interface ConditionalOnCloudPlatform {
    
}

10.6.10 @ConditionalOnSingleCandidate

@ConditionalOnSingleCandidate:组合@Conditional注解,当指定的class在容器中只有一个Bean,或者同时有多个但为首选时才开启配置。

源码注解:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnSingleCandidate {
    
}

10.7 @ConfigurationProperties

@ConfigurationProperties:用来方便加载额外的配置文件中的参数值(application.properties)。

注解源码:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
    
}

注意:在SpringBoot2.2.1中,该注解的扫描支持默认不启用,需要添加@ConfigurationPropertiesScan注解,显式地去扫描以完成初始化动作。

10.8 @EnableConfigurationProperties

该注解一般要配合@ConfigurationProperties注解使用,用来开启对@ConfigurationProperties注解配置Bean的支持。

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesRegistrar.class)
public @interface EnableConfigurationProperties {
    
}

10.9 @AutoConfigureAfter

该注解用在自动配置类上面,表示该自动配置类需要在另外指定的自动配置类配置完之后。

// Mybatis 的自动配置类,需要在数据源自动配置类之后
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration {

}

注解源码:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Documented
public @interface AutoConfigureAfter {
    
}

10.10 @AutoConfigureBefore

该注解和@AutoConfigureAfter注解使用相反,表示该自动配置类需要在另外指定的自动配置类配置之前。

注解源码:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Documented
public @interface AutoConfigureBefore {
    
}

10.11 @Import

@Import是Spring3.0添加的新注解,用来导入@Configuration注解修饰的类,通过快速导入的方式实现把实例加入spring的IOC容器中。也可以导入第三方包。

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
    
}

10.12 @ImportResource

这是 Spring 3.0 添加的新注解,用来导入一个或者多个Spring配置文件,这对SpringBoot兼容老项目非常有用,因为有些配置无法通过 Java Config 的形式来配置就只能用这个注解来导入。

注解源码:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface ImportResource {
    
}

10.13 @Component

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注,可以作用在代码的任何层次。

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
    
}

@Component@Service@Controller@Repository都是Spring注解,注解后可以被Spring框架所扫描并注入到Spring容器来进行管理。用这些注解对应用进行分层之后,就能将请求处理,业务逻辑处理,数据库操作处理分离出来,为代码解耦,也方便了以后项目的维护和开发。

  • @Component是通用注解,其他三个注解是这个注解的拓展,并且具有了特定的功能。
  • @Repository注解在持久层中,具有将数据库操作抛出的原生异常翻译转化为spring的持久层异常的功能。
  • @Controller层是SpringMvc的注解,具有将请求进行转发,重定向的功能。
  • @Service层是业务逻辑层注解,这个注解只是标注该类处于业务逻辑层。

10.11.1 @Repository

@Repository主要作用于持久化层,用来标注数据访问的DAO组件,该注解除了自动将标识的类识别为Bean,同时也能将所标注的类中抛出的数据访问异常封装为Spring的数据访问异常类型。被@Repository注解的类可以自动的被@ComponentScan 通过路径扫描给找到。

注解源码:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    
}

10.13.2 @Service

@Service主要用于标注业务层组件,表明该类处于业务逻辑层。目前该注解与@Component相同。

注解源码:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    
}

10.13.3 @Controller

@Controller用于标注是控制层组件,需要返回页面时请用。目前该注解与@Component相同。

注解源码:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    
}

10.14 @RestController

@RestController用于标注控制层组件,作用等同于@Controller + @ResponseBody

注解源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    
}

注意使用@RestController这个注解,就不能返回HTML页面,视图解析器无法解析。

10.14.1 @ResponseBody

@ResponseBody表示将 Controller 方法返回的对象,通过适当的转换器转换为指定的格式之后直接写入HTTP Response Body中,一般在异步获取数据时使用。使用@ResponseBody后返回结果不会被解析为跳转路径,会直接返回json数据。

注解源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {

}

10.14.2 @RequestBody & @RequestParam

@RequestBody主要用来接收前端传递给后端的JSON字符串中的数据,即请求体中的数据。

  • GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
  • SpringBoot2后端的接收方法里,@RequestBody@RequestParam可以共存。
  • 同一个请求中,最多只能有一个@RequestBody,而可以同时有多个@RequestParam参数。

当同时使用@RequestParam@RequestBody时,

  • @RequestParam指定的参数可以是普通元素、数组、集合、对象等等。即原SpringMVC接收参数的机制不变。
  • 如果参数时放在请求体中,传入后台的话,那么后台要用@RequestBody才能接收到。
  • 如果参数不放在请求体中的话,那么后台要用@RequestParam来接收参数。
  • 如果参数前写了@RequestParam(xxx),那么前端必须有对应的xxx名字才行(不管其是否有值,当然可以通
    过设置该注解的required属性来调节是否必须传),如果没有xxx名的话,那么请求会出错,报400。
  • 如果参数前不写@RequestParam(xxx)的话,前端如果有xxx名的参数的话,那么就会自动匹配;如果没有的话,请求也能正确发送。

当后端参数是一个以@RequestBody修饰的对象,那么前端传递JSON数据注意事项:

  • @RequestBody注解对应的类在将HTTP的输入流(含请求体)装配到目标类,即:会根据请求体中JSON字符串中的key来匹配对应实体类的属性。

  • JSON字符串中,如果value为"“的话,后端对应属性如果是String类型的,那么接受到的就是”",如果是后端属性的类型是Integer、Double等类型,那么接收到的就是null。

  • JSON字符串中,如果value为null的话,后端对应收到的就是null。

  • 如果某个参数没有value的话,在传JSON字符串给后端时,要么干脆就不把该字段写到json字符串中;要么写value时, 必须有值,null 或""都行。

注解源码:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
    
}


@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    
}

10.15 @Autowired

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

注解源码:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    
}

10.16 @RequestMapping

@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。其包含的六个属性如下:

  • params:指定请求中必须包含某些参数值时才让该方法处理。

  • headers:指定请求中必须包含某些指定的header值,才能让该方法处理请求。

  • value:指定请求的实际地址,指定的地址可以是URI Template模式 。

  • method:指定请求的类型:GET、POST、PUT、DELETE等。

  • consumes:指定处理请求的提交内容类型(Content-Type):application/jsontext/html

  • produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。

新注解主要有如下几个,他们都是组合注解

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping

相当于@RequestMapping(value=”/”,method=RequestMethod.Get\Post\Put\Delete等) 。

注解源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    
}

10.17 @RequestParam

@RequestParam:相当于 request.getParameter(),将请求参数绑定到控制器的方法参数上。

语法:

@RequestParam(value="name", required = true, defaultValue="zhangsan") String name
  • value:参数名

  • required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。

  • defaultValue:默认参数值,如果设置了该值,required=true将失效;设置为false,如果没有传该参数,就使用默认值。

注解源码:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    
}

10.18 @PathVariable

@PathVariable:通过该注解可以将URL中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。参数与大括号里的名字相同的话,注解后括号里的内容可以不填。

注解源码:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章