如何解决Java Spring从文件和命令行解析属性
伙计们!我有一个使用Spring(不是Spring Boot)的项目。像这样将一些配置传递给我的Java应用程序:
java -jar myjar.jar -conf conf.properties -D some.property=some_value another.property=another_value
我还有application.properties
文件。为了启动应用程序,我需要按照以下步骤从这些属性源中将值注入到某些服务(需要some.property
值)中:
- 尝试在CLI参数中找到
some.property
- 如果CLI不包含
some.property
,请在conf.properties
中查找 - 如果也没有
some.property
,请从application.properties
取走
我实现了自定义CommandLinePropertySource
,该自定义-D
处理.properties
标志之后的数据。现在仍然可以解析PropertySourcesPlaceholderConfigurer
文件。据我了解,我需要实例化 ResourceLoader resourceLoader = new DefaultResourceLoader();
List<Resource> resources = new ArrayList<>();
for (String filename : filesToParse) {
resources.add(resourceLoader.getResource(filename));
}
candlePropertySourcesPlaceholderConfigurer.setLocations(resources.toArray(new Resource[0])); // guy who should parse files.
context.registerBean(candlePropertySourcesPlaceholderConfigurer.getClass(),candlePropertySourcesPlaceholderConfigurer);
并传递这样的属性源:
return new Promise((resolve,reject) => {
const reader = new FileReader();
reader.onload = e => resolve(e.target.result);
reader.onerror = reject;
reader.readAsDataURL(file); // file is a jpeg file
});
它奏效了。 但是我真的不喜欢这种解决方案。对于我来说,它看起来过于设计。 您将如何实现这样的问题?也许对我的解决方案有一些评论?
P.S 这种通过命令行传递参数的方式是我的院士建立的。我知道,您想更改它,但不允许这样做。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。