在Spring Boot中有条件地应用@EnableJms。

huangapple go评论94阅读模式
英文:

Apply @EnableJms conditionally in Springboot

问题

给定以下 Spring/Boot 应用程序

    @SpringBootApplication
    @Configuration
    @ComponentScan
    @EnableGlobalMethodSecurity(
            prePostEnabled = true,
            securedEnabled = false,
            jsr250Enabled = false)
    
    @EnableJms // 我们希望能够通过应用程序属性控制此功能的开关
    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` 条目将其关闭
英文:

Given the following spring/boot application.

@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);
    }
}

We are finding that for some local debugging scenarios we want @EnableJms turned off. We comment out the line of code. We are occasionally creating PR(s) with the line commented. The PR are being caught by code review.

It's going to slip through one day. How we control @EnableJms from an application property file, preferably that it's on by default, but can be turned off with an application-local.properties entry.

答案1

得分: 5

创建一个新的类,并使用以下三个注解进行标记:

@Configuration
@EnableJms
@ConditionalOnProperty(name = "turnonjms", havingValue = "true")
public class MyEnableJmsCustomConfig {
    //你可以保持它为空。只要确保这个类
    //与主类在同一个文件夹中
}

你将会从外部来源传递 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  # 如果不起作用,将 `true` 放在双引号中
英文:

Create a new class and mark it with these 3 annotation:

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

You will pass turnonjms property from external sources like runtime args --turnonjms=true or -Dturnonjms. If it is present, only then @EnableJms will be active. Otherwise it will be off.

Or if you like, you can always have JMS enabled and turn it off only when certain external property is present:

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

If you don't pass turnoffjms property from external sources, then you will always have JMS enabled. If you pass --turnoffjms=true, JMS will be disabled.

On a side note, When you use @SpringBootApplication, it already has @configuration and @ComponentScan annotations within it. You would want to use @ComponentScan if you were to scan folders outside of current package. EnableGlobalMethodSecurity(...) also has embedded @Configuration, so it is safe to remove these two annotation from your main class.

Edit:

Since you are already using application-local.properties, insert this entry to turn it off:

turnoffjms: true # put true in double quotes if it doesn't work

huangapple
  • 本文由 发表于 2020年4月8日 15:44:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61095643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定