[英]Are there anyway to disable annotation in spring4?
I have a question, maybe simple, but I can not find out the solution.我有一个问题,也许很简单,但我找不到解决方案。
I am using spring boot and added some annotation to the code like this:我正在使用 spring boot 并在代码中添加了一些注释,如下所示:
@EnableEurekaClient
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
But in some other environment, for example, in production environment, we want to remove EurekaClient, but I do not want to manually remove it manually for each environment, instead, I want to use environment variable or command line parameter to control the behavior.但是在其他一些环境中,比如生产环境中,我们想要移除EurekaClient,但我不想每个环境都手动移除它,而是想使用环境变量或命令行参数来控制行为。 I suppose to do this way:我想这样做:
@EnableEurekaClient(Enabled = {EnableEureka})
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Then I can easily start this application without touching the code.然后我可以轻松启动这个应用程序而无需接触代码。
Can anyone tell me if this is possible?谁能告诉我这是否可能? If so, how can I do it?如果是这样,我该怎么做?
Thanks谢谢
You would want to work with Spring Boot Profiles .您可能希望使用Spring Boot Profiles 。 Split out the @EnableEurekaClient
to another @Configuration
class and also add an @Profile("eureka-client")
to the class.将@EnableEurekaClient
拆分为另一个@Configuration
类,并将@Profile("eureka-client")
到该类中。 Then when starting up the application you can set a -Dspring.profiles.active=eureka-client
for the environments other than production.然后在启动应用程序时,您可以为生产以外的环境设置-Dspring.profiles.active=eureka-client
。
Example:例子:
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Configuration
@EnableEurekaClient
@Profile("eureka-client")
public class EurekaClientConfiguration {
}
I prefer this method as you don't have to create an extra profile:我更喜欢这种方法,因为您不必创建额外的配置文件:
@Configuration
@EnableEurekaClient
@ConditionalOnProperty(name = "application.enabled", havingValue = "true", matchIfMissing = false)
public class EurekaClientConfiguration {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.