Spring 互斥bean加载
我正在使用SpringBoot,并希望根据传递的配置文件有条件地加载两个beanSpring 互斥bean加载,spring,spring-boot,Spring,Spring Boot,我正在使用SpringBoot,并希望根据传递的配置文件有条件地加载两个bean @Configuration @Profile("secure") public class Secured ... //this should only load when "secure" is supplied @Configuration public class NotSecured ... //this should be the default 所以基本上: 如果用户传递了--spring.pro
@Configuration
@Profile("secure")
public class Secured ... //this should only load when "secure" is supplied
@Configuration
public class NotSecured ... //this should be the default
所以基本上:
如果用户传递了--spring.profiles.active=secured
我希望加载受保护的bean,但不希望加载不受保护的bean。默认情况下,它应该只加载NotSecured bean
这可能吗?您可以使用“!”not运算符,即用
@Profile(“!secure”)
注释Bean/Configuration类,并且仅当“secure”配置文件未激活时才使用它 您也可以使用此注释明确指定默认配置文件设置:
@Profile(“默认”)
- 或者使用类似于
@Profile({“unsecure”,“default”}
@Primary
和@Profile(…)
注释的,那么如果配置文件未配置,则带有默认配置文件的bean将被注入(示例配置applications.properties
:spring.profiles.active=val1,val2,…
)
换言之,当在任何实现中使用@Profile
时,似乎不会考虑@Primary
注释;如果未定义配置文件,则将使用默认配置文件(可能是没有@Profile
注释的实现,因为该注释成为默认注释!)
spring启动程序父级
2.2.2.1发布
用户通过是什么意思--spring.profiles.active=secured?这就是如何在spring应用程序中设置当前配置文件的方法,我指的是其他开发人员。@minion很好,根据这一点,你是如何做到的。但是它不起作用。当你指的是用户通过时,我照字面意思理解了。这就是我的问题的困惑所在.