zoukankan      html  css  js  c++  java
  • 【SpringBoot 注解】合集

    参考: https://www.baeldung.com/spring-boot-annotations

    @SpringBootApplication

    mark the main class of a Spring Boot application:

    @SpringBootApplication encapsulates @Configuration@EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.

    @EnableAutoConfiguration

    @EnableAutoConfiguration enables auto-configuration. It means that Spring Boot looks for auto-configuration beans on its classpath and automatically applies them.

    we have to use this annotation with @Configuration:

    @Configuration
    @EnableAutoConfiguration
    class VehicleFactoryConfig {}

    Auto-Configuration Conditions

    when we write our custom auto-configurations, we want Spring to use them conditionally. We can place the annotations in this section on @Configuration classes or @Bean methods.

    @ConditionalOnClass and @ConditionalOnMissingClass

    Using these conditions, Spring will only use the marked auto-configuration bean if the class in the annotation's argument is present/absent:

    @Configuration
    @ConditionalOnClass(DataSource.class)
    class MySQLAutoconfiguration {
        //...
    }

    @ConditionalOnBean and @ConditionalOnMissingBean

    @Bean
    @ConditionalOnBean(name = "dataSource")
    LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        // ...
    }

    @ConditionalOnProperty

    @Bean
    @ConditionalOnProperty(
        name = "usemysql", 
        havingValue = "local"
    )
    DataSource dataSource() {
        // ...
    }

    @ConditionalOnResource

    @ConditionalOnResource(resources = "classpath:mysql.properties")
    Properties additionalProperties() {
        // ...
    }

    @ConditionalOnWebApplication and @ConditionalOnNotWebApplication

    @ConditionalOnWebApplication
    HealthCheckController healthCheckController() {
        // ...
    }

    @ConditionalExpression

    @Bean
    @ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")
    DataSource dataSource() {
        // ...
    }

    @Conditional

     we can create a class evaluating the custom condition. We tell Spring to use this custom condition with @Conditional:

    @Conditional(HibernateCondition.class)
    Properties additionalProperties() {
        //...
    }
  • 相关阅读:
    Telnet远程测试
    数据库笔记
    gcc 链接不到 函数实现, undefined reference to xxx
    usb2ttl 引脚定义
    ip v4 地址中 局域网地址范围
    vdi 磁盘文件转换为 vmdk文件的命令
    tftp 命令使用
    无法通过vnc连接到局域网内的树莓派
    镜像服务网站
    C语言 scanf 输入浮点数的用法
  • 原文地址:https://www.cnblogs.com/clarino/p/15481994.html
Copyright © 2011-2022 走看看