• 关于Springboot的@Profile的写法以及多个profile的写法排坑(“!profile1 && !profile2“ 的写法)


    基于如下的版本得到的结论

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.3version>
        <relativePath/> 
    parent>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    一个接口两个实现类,根据不同的profile来实例化不同的bean

    结论1:profile名字的匹配原则是大小写敏感
    例子1:不指定任何profile,默认名为 “default” 的profile(存在名为default的profile!!!)
    // 不指定任何profile,结论是default的生效,改成 @Profile({"Default"}) 不指定任何profile的时候启动失败,因为匹配不到任何profile
    @Profile({"profile1"})@Profile({"default"})
    
    • 1
    • 2
    例子2:启动时指定的profile有多个,只要含有其中任何一个就能生效
    // 如启动时指定多个profile:--spring.profiles.active=abc,def,则如下是可以生效的
    @Profile({"abc"})   (很显然,标注为  @Profile({"def"} 的也生效)
    • 1
    • 2
    例子3:多个profile的意思,是"或"的关系
    // 启动时指定的profile包含有profile1或profiles的时候实例化
    @Service
    @Profile({"profile1", "profile2"})
    public class PrinterInk implements Printer {
    }
    
    // "不包含profile1或不包含profile2的时候实例化
    @Service
    @Profile({"!profile1", "!profile2"})
    public class PrinterLaser implements Printer {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    认真思考,这两个不是互斥的,用数学语言表示就是 profile1 OR profile2!profile1 OR !profile2 是非互斥、非互为补集的!!!

    profile1 OR profile2 的对立面(补集)是 !profile1 AND !profile2

    所以,当你启动springboot时指定的profile不等于profile1且不等于profile2的时候才能启动(否则2个候选bean)
    指定 --spring.profiles.active=profile1,profile2 会让PrinterInk生效PrinterLaser失效,能顺利启动springboot

    如何让两个类配置的@Profile互斥?,请继续看

    例子4:如何使用 “且”,是否可以改成 @Profile({“!profile1 && !profile2”}) 吗?(低版本不支持)

    是的,可以!! 这两种写法实践证明是等价的 @Profile({"!profile1 && !profile2"})@Profile({"!profile1 & !profile2"})

    低版本里不能使用 "!profile1 && !profile2" 的写法,你可以使用替代的方案

    据说springboot引入的spring 5.x 之后才支持?我这个例子使用的springboot 的starter是2.7.3的,引入的spring是5.3.22版本的。

    **另外:**低版本springboot不支持 “!profile1 && !profile2” 的写法并不会直接报错,会比较隐晦难发现!!!—据说从 Spring 5.1 开始才支持这种写法

    @Service
    @Profile({"profile1", "profile2"})
    public class PrinterInk implements Printer {
    }
    
    // 只有 PrinterInk 这个bean 缺失的时候才会实例化
    @Service
    @ConditionalOnMissingBean(PrinterInk.class)
    public class PrinterLaser implements Printer {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    Postgresql在jdbc处理bit字段的解决方案
    2018年计网408
    k8s驱逐篇(2)-kubelet节点压力驱逐
    二叉树题目:二叉树寻路
    【华为OD机试】HJ26 字符串排序
    基于AD Event日志检测NTDS凭据转储攻击
    linux应用移植问题
    第10章 MyBatisPlus实现分页检索
    【论文阅读】Generating Radiology Reports via Memory-driven Transformer (EMNLP 2020)
    【Linux内核源码剖析】进程原理及系统调用
  • 原文地址:https://blog.csdn.net/w8y56f/article/details/126653489