无论如何在spring4中禁用注释? [英] Are there anyway to disable annotation in spring4?
问题描述
我有一个问题,也许很简单,但是我找不到解决方法.
I have a question, maybe simple, but I can not find out the solution.
我使用的是Spring Boot,并在代码中添加了一些注释,如下所示:
I am using spring boot and added some annotation to the code like this:
@EnableEurekaClient
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
但是在某些其他环境中,例如在生产环境中,我们想要删除EurekaClient,但是我不想为每个环境手动删除它,而是希望使用环境变量或命令行参数来控制行为.我想这样做:
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. 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?
谢谢
推荐答案
您希望使用.然后,在启动应用程序时,可以为生产环境以外的环境设置 -Dspring.profiles.active = eureka-client
.
You would want to work with Spring Boot Profiles. Split out the @EnableEurekaClient
to another @Configuration
class and also add an @Profile("eureka-client")
to the class. Then when starting up the application you can set a -Dspring.profiles.active=eureka-client
for the environments other than production.
示例:
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Configuration
@EnableEurekaClient
@Profile("eureka-client")
public class EurekaClientConfiguration {
}
这篇关于无论如何在spring4中禁用注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!