spring 具有多个PropertySourcesPlaceholderConfigurerbean的Sping Boot 应用程序解析为属性的默认值,而不是配置值

rkttyhzu  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(56)

我有一个spring Boot 应用程序,我使用@Value("${placeholder-config-path:default-value}")将值注入到bean中。我观察到每次加载属性的default-value,而不是解析来自URL的placeholder-config-path。当我不提供默认值时,加载正确的值。
在研究这个问题时,我发现当应用程序上下文中存在多个PropertySourcesPlaceholderConfigurer bean时会发生这个问题。我检查了使用/actuator/beans路径生成的bean,注意到有两个PropertySourcesPlaceholderConfigurer bean示例具有相同的id和singleton作用域。
在运行时调试代码时,我注意到PropertySourcesPlaceholderConfigurer bean(spring-beans v5.2.27)只在没有提供默认值的情况下才解析占位符路径。ignoreUnresolvablePlaceholders标志被设置为true,因此,理想情况下,尽管有多个configurer bean,占位符最终应该被解析。
如果configurer bean的多个示例确实是根本原因,我该如何解决这个问题?我没有显式地创建这些bean。它们可能会在我从Spring cloud以及从本地application. yml加载示例时被示例化。有没有一种方法可以合并组合这两个示例?我可以通过不使用默认值来暂时解决这个问题,但是我希望能够使用默认值。
编辑:以下是来自/actuator/beans端点的响应的一部分:作为/actuator/beans响应的一部分,我看到以下内容:

{
"contexts": {
    "my-service-1": {
        "beans": {
            
            "propertySourcesPlaceholderConfigurer": {
                "aliases": [],
                "scope": "singleton",
                "type": "org.springframework.context.support.PropertySourcesPlaceholderConfigurer",
                "resource": "class path resource [org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfiguration.class]",
                "dependencies": []
            }
        
    },
    "bootstrap": {
        "beans": {
            
            
            "propertySourcesPlaceholderConfigurer": {
                "aliases": [],
                "scope": "singleton",
                "type": "org.springframework.context.support.PropertySourcesPlaceholderConfigurer",
                "resource": "class path resource [org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfiguration.class]",
                "dependencies": []
            }
    }
}

字符串

rfbsl7qr

rfbsl7qr1#

我发现其中一个依赖jar通过xml配置引入了一个PropertyPlaceholderConfigurer示例。每当需要解析占位符时,请求都会转到没有所需占位符配置的PropertyPlaceholderConfigurer示例。因此,它总是解析为默认值。我不再需要jar,因此我删除了修复此问题的依赖项。

相关问题