zoukankan      html  css  js  c++  java
  • springboot 核心注解

    Spring boot 使用也需要搭配 Spring 使用 ,Spring 常用的注解

    Spring 注解

    1.@Configuration

    @Configuration 注解用户定义配置类,可替换xml 文件,被注解的类包含一个或者多个 @Bean 注解的方法,这些方法将被 ,用于构建Bean ,初始化Spring 容器

    2.@ComponentScan

    常用的注解 @Controller @Service @Repository ,有一个共同的注解 @Component ,@ComponentScan 标注的就会扫描这些注解标注的类到Spring 容器中

    @SpringBootApplication 注解就包含了 @ComponentScan 注解,即不用再添加扫描注解【@EnableAutoConfiguration @ComponentScan @SpringBootConfiguration】

    3.@Conditional

    @Conditional 是 Spring 4 提供的注解,通过@Conditional 注解可以根据设置的条件装载不同的bean ,Springboot 注解中的 @ConditionalOnProperty @ConditionalOnBean  等以@Conditional* 

    开头的注解,都是通过集成了 @Conditional 来实现相应的功能的

    4.@Import

    通过导入的方式把实例加入到Spring IOC 容器中

    5.@ImportResource

    与 @Import 类似,区别是 @ImportResource 导入的是配置文件

    6.@Component

    @Component 是一个元注解,带有该注解的类被看作组件,当基于注解的类路径扫描的时候,这些类就会被实例化

    Spring boot 核心注解

    1. @SpringBootApplication

    是SpringBoot 的最核心的注解,在spring boot 的主类上,标识 是SpringBoot 应用,用来开启SpringBoot 的各项能力。由@SpringBootConfiguration @EnableAutoConfiguration  @ComponentScan 三个注解组成。这三个注解是一起使用,

    所以spring boot提供了一个统一的注解 @SpringBootApplication

    2.@EnableAutoConfiguration

    允许Springboot 自动装配,开启改注解,Spring boot 就能根据当前类路径下的包或者类来配置 Spring Bean 

    例如:当前类路径下有 Mybatis 的 JAR 包,MybatisAutoConfiguration 注解就能根据相关的参数来配置Mybatis 的各个Spring Bean

    @EnableAutoConfiguration  实现的关键在于引入了AutoConfigurationImportSelector ,其核心逻辑为 selectImports 方法,

    • 从配置文件 MATA-INF/spring.factories 加载所有可能用到的自动装配类
    • exclude excludeName 属性携带的类排除
    • 过滤,将满足条件 @Conditional 的自动配置类返回

    3.@SpringBootConfiguration

    改注解就是 @Configuration 注解的变体,用来修改SpringBoot 配置

    4.@ConditionalOnBean

    @ConditionalOnBean(A.class)  当前上下文存在A对象时,才会实例化一个 Bean ,也就是只有A.class ,在 Spring 的 上下文中时,当前的 bean 才能够创建

    5.@ConditionalOnMissingBean

    与上述相反,当缺失某个 bean 才会创建当前的bean 

    6.@ConditionalOnClass

    当且仅当某些类存在于 classpath 上,才会创建某个 bean

    7.@ConditionalOnMissingClass

    与上述相反,当前仅当classpath 不存在指定的Class 才会开启配置

    8.@ConditionalOnProperty

    指定的属性有指定的值才开启配置,通过属性 name 以及havingValue ,其中 name 用 application.properties 中读取某个属性的值

    9.@ConditionalOnResource

    classpath 类路径下有指定的资源才会开启配置

     10.@PropertySource(“”)

    指定配置文件路径

    11.@ConfigurationProperties(prefix = "前缀")

    指定配置文件前缀

  • 相关阅读:
    LCA算法总结
    【福利】论机房如何关闭方正软件保护卡
    codevs 2190 有理逼近
    用C语言的rand()和srand()产生伪随机数的方法总结
    float,double等精度丢失问题 float,double内存表示
    #incldue<cctype>函数系列
    poj 2348 Euclid's Game 题解
    poj 2240 Arbitrage 题解
    洛谷 p1352 没有上司的舞会 题解
    BZOJ 1093 最大半连通子图 题解
  • 原文地址:https://www.cnblogs.com/bytecodebuffer/p/13678945.html
Copyright © 2011-2022 走看看