Java 运行时未使用Spring启动属性源
我有一个非常小的Spring引导程序,它试图在相关类中添加自己的属性Java 运行时未使用Spring启动属性源,java,spring-boot,properties-file,Java,Spring Boot,Properties File,我有一个非常小的Spring引导程序,它试图在相关类中添加自己的属性 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.
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);
}
}
package com.findology.testboot2;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.boot.web.server.WebServerFactoryCustomizer;
导入org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.PropertySource;
导入org.springframework.stereotype.Component;
@配置
@PropertySource({/*“classpath:/application.properties”、*/“classpath:/countrycounts.properties”})
@组成部分
公共类CountryCountsWebSetup实现WebServerFactoryCustomizer{
@值(${countries.server.port}”)
国际港口;
@凌驾
public void自定义(ConfigurableServletWebServerFactory服务器){
server.setPort(端口);
}
}
和一个属性文件
countrycounts.kafka.bootstrapserver=
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\u COUNTS\u WINDOWS
countrycounts.kafka.listener.id=country\u counts\u窗口
countrycounts.server.port=8989
我遇到的问题是,在启用调试的情况下运行时,Spring Boot似乎会查找并加载countrycounts.properties
文件
2018-08-29 17:14:10.697[main;MutablePropertySources]调试--添加PropertySource“类路径资源[countrycounts.properties]”,搜索优先级立即高于“类路径资源[f2 traffic.properties]”
但无法完成启动,因为它声称缺少countrycounts.server.port
属性
2018-08-29 17:14:12.705[main;AbstractApplicationContext]警告--上下文初始化期间遇到异常-取消刷新尝试:org.springframework.context.ApplicationContextException:无法启动web服务器;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“JettyServletWebServerFactory”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedJetty.class]中定义:初始化bean失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“countryCountsWebSetup”的bean时出错:自动连线依赖项的注入失败;嵌套异常为java.lang.IllegalArgumentException:无法解析值“${countrycounts.server.port}”中的占位符“countrycounts.server.port”
如果我使用-D
在java命令行上设置该值,它可以正常工作,但仍然意味着没有从文件中读取该属性
知道为什么会加载属性文件,但不使用其中的值吗
更新
作为该程序的maven依赖项的一部分,包含了一个大型(复杂)jar文件,它使用Spring 4.1,包括一个
propertyPlaceHolder
,它还定义了位置。这是否会破坏我需要的属性文件的路径,从而导致无法看到这些属性?我解决了这个问题,但不是以一种非常干净的方式。我不喜欢这个解决方案,但它似乎有效
为了解决这个问题,我从jar文件依赖项中提取了定义xml文件的嵌入式属性。然后,我从中删除了占位符属性
,并创建了一个新文件,其中包含指向我的目录的新占位符属性。然后在我的新xml文件中添加一个ImportSource
,并确保使用大jar文件的类显式地包含它所需的复制和修改的属性/xml文件
在那一点上,我的属性被看到了,一切都运行得很好,但我认为这是一个很大的难题,我仍然希望找到一种真正的Spring Boot方法来干净地完成这项工作。为什么不利用Spring Boot的属性配置?为什么有两个属性文件并手动映射它们?最可能的问题是如何映射countrycounts.properties,f2-traffic.properties是否也包含相同的值?将此复选为well@AmitPhaltankar我尝试将属性值放入application.properties
,并让它们自动加载,但没有成功either@AntJavaDev我使用的属性仅存在于countrycounts.properties
文件中,因此没有覆盖。你能解释一下映射countrycounts.properties是什么意思吗关于你提到的答案,我尝试了所有这些,但没有一个有效。映射属性是指你如何引用文件,以及在运行时类路径上是否可用。我猜您正在创建一个可运行的jar(SpringBoot应用程序),因此如果您可以再次检查所有文件是否正确打包在jar中。