java 在 Spring Boot 中使用 @Profile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25427684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:01:31  来源:igfitidea点击:

Using @Profile in spring boot

javaspringspring-mvcspring-boot

提问by Jan Zyka

I have spring boot application (1.1.5.RELEASE) and enabling my profiles via the configuration protperty spring.profiles.active=MyProfile

我有 spring boot 应用程序(1.1.5.RELEASE)并通过配置属性启用我的配置文件spring.profiles.active=MyProfile

The profile gets activated correctly which I can see by beans from that profile being created.

配置文件被正确激活,我可以从正在创建的配置文件中看到 bean。

Then I have a @Controllerused as follows:

然后我有一个@Controller使用如下:

@Controller
@RequestMapping("/someUrl")
@Profile("MyProfile")
public class MyController {
...
}

This controller is not instantiated and URL used in the controller are not mapped. In the same package I have another controllers which are not limited by @Profileand these get instsantiated and mapped as expected.

此控制器未实例化,控制器中使用的 URL 未映射。在同一个包中,我有另一个不受限制的控制器,@Profile这些控制器按预期进行实例化和映射。

So is using @Profileannotation on controller something which is not compatible with spring boot? Is there other approach I should be using?

那么@Profile在控制器上使用注释是否与 Spring Boot 不兼容?我应该使用其他方法吗?

Edit:It seems to be a bug after all as if I include -Dspring.profiles.active=MyProfileas JVM property the controller gets instantiated :'(

编辑:毕竟这似乎是一个错误,就好像我-Dspring.profiles.active=MyProfile将控制器作为 JVM 属性包含在内一样:'(

Edit2:So here comes the interesting part:

Edit2:所以有趣的部分来了:

  • If you define spring.profiles.activein application.propertieswhich is loaded by default from classpath thne it works

  • when you rename the file to test.propertiesand include it via @PropertySource("classpath:test.properties")it stops working. Will raise a bug against it.

  • 如果定义spring.profiles.activeapplication.properties其默认情况下,从classpath中加载thne它的工作原理

  • 当您将文件重命名为test.properties并通过@PropertySource("classpath:test.properties")它包含它时,它将停止工作。会提出一个错误。

Edit 3:As promised: https://github.com/spring-projects/spring-boot/issues/1417

编辑 3:如承诺:https: //github.com/spring-projects/spring-boot/issues/1417

Thanks!

谢谢!

采纳答案by Andy Wilkinson

I've tracked this down to what I believe to be a bug in Spring. See SPR-12111for more details.

我已经将其追溯到我认为是 Spring 中的一个错误。有关更多详细信息,请参阅SPR-12111

回答by gyoder

You can definitely annotate a controller with @Profile in Spring Boot, just as you are doing above. MyController gets instantiated if MyProfile is active. Are you sure that "MyProfile" is the active profile? Are you setting the spring.profiles property?

您绝对可以在 Spring Boot 中使用 @Profile 注释控制器,就像您在上面所做的那样。如果 MyProfile 处于活动状态,则 MyController 会被实例化。您确定“MyProfile”是活动配置文件吗?你在设置 spring.profiles 属性吗?

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/context/annotation/Profile.htmlThe @Profile annotation may be used in any of the following ways:

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/context/annotation/Profile.html@Profile 注释可以通过以下任何一种方式使用:

as a type-level annotation on any class directly or indirectly annotated with @Component, including @Configuration classes as a meta-annotation, for the purpose of composing custom stereotype annotations

作为任何直接或间接用@Component 注释的类的类型级注释,包括作为元注释的 @Configuration 类,用于组合自定义构造型注释