给出下面的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
条目将其关闭。
创建一个新类,并使用以下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
批注。如果要扫描当前软件包以外的文件夹,则需要使用@ComponentScan
。 EnableGlobalMethodSecurity(...)
还嵌入了@Configuration
,因此可以从主类中删除这两个注释。
编辑:
由于您已经在使用application-local.properties,请插入此条目以将其关闭:
turnoffjms:true