在 Spring Boot 中使用 @Profile [英] Using @Profile in spring boot
问题描述
我有 spring 启动应用程序 (1.1.5.RELEASE) 并通过配置属性启用我的配置文件 spring.profiles.active=MyProfile
I have spring boot application (1.1.5.RELEASE) and enabling my profiles via the configuration protperty spring.profiles.active=MyProfile
配置文件被正确激活,我可以从正在创建的配置文件中看到 bean.
The profile gets activated correctly which I can see by beans from that profile being created.
然后我有一个 @Controller
使用如下:
Then I have a @Controller
used as follows:
@Controller
@RequestMapping("/someUrl")
@Profile("MyProfile")
public class MyController {
...
}
此控制器未实例化,控制器中使用的 URL 未映射.在同一个包中,我有另一个不受 @Profile
限制的控制器,这些控制器按预期进行实例化和映射.
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 @Profile
and these get instsantiated and mapped as expected.
那么在控制器上使用 @Profile
注释是否与 Spring Boot 不兼容?我应该使用其他方法吗?
So is using @Profile
annotation on controller something which is not compatible with spring boot? Is there other approach I should be using?
毕竟这似乎是一个错误,好像我包含 -Dspring.profiles.active=MyProfile
作为控制器被实例化的 JVM 属性:'(
It seems to be a bug after all as if I include -Dspring.profiles.active=MyProfile
as JVM property the controller gets instantiated :'(
Edit2:有趣的部分来了:
如果你在
application.properties
中定义了spring.profiles.active
,它默认从类路径加载,它可以工作
If you define
spring.profiles.active
inapplication.properties
which is loaded by default from classpath thne it works
当您将文件重命名为 test.properties
并通过 @PropertySource("classpath:test.properties")
包含它时,它会停止工作.将针对它提出错误.
when you rename the file to test.properties
and include it via @PropertySource("classpath:test.properties")
it stops working. Will raise a bug against it.
编辑 3: 正如所承诺的:https://github.com/spring-projects/spring-boot/issues/1417
谢谢!
推荐答案
我已经追踪到我认为是 Spring 中的一个错误.有关详细信息,请参阅 SPR-12111.
I've tracked this down to what I believe to be a bug in Spring. See SPR-12111 for more details.
这篇关于在 Spring Boot 中使用 @Profile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!