基于如下的版本得到的结论
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.3version>
<relativePath/>
parent>
一个接口两个实现类,根据不同的profile来实例化不同的bean
// 不指定任何profile,结论是default的生效,改成 @Profile({"Default"}) 不指定任何profile的时候启动失败,因为匹配不到任何profile
@Profile({"profile1"}) 和 @Profile({"default"})
// 如启动时指定多个profile:--spring.profiles.active=abc,def,则如下是可以生效的
@Profile({"abc"}) (很显然,标注为 @Profile({"def"} 的也生效))
// 启动时指定的profile包含有profile1或profiles的时候实例化
@Service
@Profile({"profile1", "profile2"})
public class PrinterInk implements Printer {
}
// "不包含profile1或不包含profile2的时候实例化
@Service
@Profile({"!profile1", "!profile2"})
public class PrinterLaser implements Printer {
}
认真思考,这两个不是互斥的,用数学语言表示就是 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互斥?,请继续看
是的,可以!! 这两种写法实践证明是等价的 @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 {
}