Spring Boot属性源在运行时未使用

问题描述 投票:2回答:1

我有一个非常小的Spring Boot程序,它试图在相关的类中添加自己的属性

package com.findology.testboot2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Configuration
@PropertySource({ /*"classpath:/application.properties",*/ "classpath:/countrycounts.properties" })
@Component
public class CountryCountsWebSetup implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

@Value("${countrycounts.server.port}")
int port;

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(port);
    }

}

和一个属性文件

countrycounts.kafka.bootstrapServers = kafka1.data.int.dc1.ad.net:9092,kafka2.data.int.dc1.ad.net:9092,kafka3.data.int.dc1.ad.net:9092,kafka4。 data.int.dc1.ad.net:9092,kafka5.data.int.dc1.ad.net:9092

countrycounts.kafka.topic = COUNTRY_COUNTS_WINDOWS

countrycounts.kafka.listener.id = country_counts_window

countrycounts.server.port = 8989

我遇到的问题是,在启动调试时运行Spring Boot似乎找到并加载countrycounts.properties文件

2018-08-29 17:14:10.697 [main; MutablePropertySources] DEBUG -- Adding PropertySource 'class path resource [countrycounts.properties]' with search precedence immediately higher than 'class path resource [f2-traffic.properties]'

但无法完成启动,因为它声称countrycounts.server.port属性丢失。

2018-08-29 17:14:12.705 [main; AbstractApplicationContext] WARN -- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JettyServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedJetty.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryCountsWebSetup': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'countrycounts.server.port' in value "${countrycounts.server.port}"

如果我使用-D在java命令行上设置值,它工作正常,但仍然暗示该属性未从文件中读取。

有没有想法为什么属性文件被加载但其中的值没有被使用?

UPDATE

作为该程序的maven依赖项的一部分,包含一个大型(和复杂的)jar文件,它使用Spring 4.1并包含propertyPlaceHolder,它还定义了location。这可能会破坏我需要的属性文件的路径,从而导致这些属性不被看到?

java spring-boot properties-file
1个回答
0
投票

我解决了这个问题但不是很干净。我不喜欢这个解决方案,但似乎有效。

为了解决这个问题,我从jar文件依赖项中提取了定义xml文件的嵌入式属性。然后我从中删除了placeholderProperties并使用指向我的目录的新placeholderProperties创建了一个新文件。然后在我的新xml文件中添加一个ImportSource,并确保使用大型jar文件的类显式包含所需的复制和修改的属性/ xml文件。

那时,我的属性被看到了,一切都运行正常,但我认为这是一个很大的问题,我仍然希望找到一个真正的Spring Boot方式来干净利落地完成这项工作。

© www.soinside.com 2019 - 2024. All rights reserved.