Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 互斥bean加载_Spring_Spring Boot - Fatal编程技术网

Spring 互斥bean加载

Spring 互斥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

我正在使用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.profiles.active=secured
我希望加载受保护的bean,但不希望加载不受保护的bean。默认情况下,它应该只加载NotSecured bean


这可能吗?

您可以使用“!”not运算符,即用
@Profile(“!secure”)
注释Bean/Configuration类,并且仅当“secure”配置文件未激活时才使用它

您也可以使用此注释明确指定默认配置文件设置:

  • @Profile(“默认”)
  • 或者使用类似于
    @Profile({“unsecure”,“default”}
值得注意的是,任何未指定概要文件的bean都属于默认概要文件

我惊讶地发现,如果一个服务有几个实现,其中一个是用
@Primary
@Profile(…)
注释的,那么如果配置文件未配置,则带有默认配置文件的bean将被注入(示例配置
applications.properties
spring.profiles.active=val1,val2,…

换言之,当在任何实现中使用
@Profile
时,似乎不会考虑
@Primary
注释;如果未定义配置文件,则将使用默认配置文件(可能是没有
@Profile
注释的实现,因为该注释成为默认注释!)


spring启动程序父级
2.2.2.1发布

用户通过是什么意思--spring.profiles.active=secured?这就是如何在spring应用程序中设置当前配置文件的方法,我指的是其他开发人员。@minion很好,根据这一点,你是如何做到的。但是它不起作用。当你指的是用户通过时,我照字面意思理解了。这就是我的问题的困惑所在.