[在Springboot中有条件地应用@EnableJms

问题描述 投票:1回答:1

给出下面的spring / boot应用程序。

@SpringBootApplication
@Configuration
@ComponentScan
@EnableGlobalMethodSecurity(
        prePostEnabled = true,
        securedEnabled = false,
        jsr250Enabled = false)

@EnableJms // we would like to control this from an application property on/off
public class PayZilchCustomerServiceApplication {
    static {
        SSLUtilities.trustAllHostnames();
        SSLUtilities.trustAllHttpsCertificates();
    }
    public static void main(String[] args) {
        SpringApplication.run(PayZilchCustomerServiceApplication.class, args);
    }
}

我们发现对于某些本地调试方案,我们希望关闭@EnableJms。我们注释掉代码行。我们有时会创建带有注释行的PR。 PR正在通过代码审查来捕获。

将要经过一天。我们如何从应用程序属性文件控制@EnableJms,最好是默认情况下它处于打开状态,但可以通过application-local.properties条目将其关闭。

java spring spring-boot spring-jms
1个回答
3
投票

创建一个新类,并使用以下3个注释进行标记:

@Configuration
@EnableJms
@ConditionalOnProperty(name = "turnonjms", havingValue = "true")
public class GiveItSomeProperName {
   //you can keep it empty. Just make sure this is present in the same folder where main class is
}

您将从诸如运行时参数turnonjms--turnonjms=true之类的外部源传递-Dturnonjms属性。如果存在,则仅@EnableJms将处于活动状态。否则它将关闭。

或者,如果您愿意,可以始终启用JMS并仅在存在某些外部属性时将其关闭:

@ConditionalOnProperty(name = "turoffjms", havingValue = "false")

如果您从外部来源传递turnoffjms属性,则将始终启用JMS。如果通过--turnoffjms=true,则JMS将被禁用。

附带说明,当您使用@SpringBootApplication时,其中已经包含@configuration@ComponentScan批注。如果要扫描当前软件包以外的文件夹,则需要使用@ComponentScanEnableGlobalMethodSecurity(...)还嵌入了@Configuration,因此可以从主类中删除这两个注释。

编辑:

由于您已经在使用application-local.properties,请插入此条目以将其关闭:

turnoffjms:true

© www.soinside.com 2019 - 2024. All rights reserved.