gpt4 book ai didi

spring - PropertySourcesPlaceholderConfigurer 未在 SpringBoot 项目中注册环境

转载 作者:行者123 更新时间:2023-12-02 08:06:51 34 4
gpt4 key购买 nike

我正在将一个工作项目从使用 SpringBoot 命令行参数转移到从文件中读取属性。以下是@Configuration类所涉及的部分:

@Configuration
class RemoteCommunication {

@Inject
StandardServletEnvironment env


@Bean
static PropertySourcesPlaceholderConfigurer placeholderConfigurer () {
// VERIFIED this is executing...
PropertySourcesPlaceholderConfigurer target = new PropertySourcesPlaceholderConfigurer()
// VERIFIED this files exists, is readable, is a valid properties file
target.setLocation (new FileSystemResource ('/Users/me/Desktop/mess.properties'))
// A Debugger does NOT show this property source in the inject Environment
target
}


@Bean // There are many of these for different services, only one shown here.
MedicalSorIdService medicalSorIdService () {
serviceInstantiator (MedicalSorIdService_EpicSoap, 'uri.sor.id.lookup.internal')
}


// HELPER METHODS...


private <T> T serviceInstantiator (final Class<T> classToInstantiate, final String propertyKeyPrimary) {
def value = retrieveSpringPropertyFromConfigurationParameter (propertyKeyPrimary)
classToInstantiate.newInstance (value)
}


private def retrieveSpringPropertyFromConfigurationParameter (String propertyKeyPrimary) {
// PROBLEM: the property is not found in the Environment
def value = env.getProperty (propertyKeyPrimary, '')
if (value.isEmpty ()) throw new IllegalStateException ('Missing configuration parameter: ' + "\"$propertyKeyPrimary\"")
value
}

使用@Value注入(inject)属性确实有效,但是如果可能的话,我宁愿直接使用Environment。如果设置不在Environment中,那么我不确定@Value从哪里提取它们......

当我传入指定属性的命令行参数时,

env.getProperty() 仍然可以正常工作。

欢迎任何建议!

最佳答案

这里的问题是 PropertySourcesPlaceholderConfigurerStandardServletEnvironment 之间的区别,或者为了简单起见,Environment 之间的区别。

Environment 是一个支持整个 ApplicationContext 的对象,并且可以解析一堆属性(Environment 接口(interface)扩展了 PropertyResolver )。 ConfigurableEnvironment 有一个 MutablePropertySources 对象,您可以通过 getPropertySources() 检索该对象。此 MutablePropertySources 包含 PropertySource 对象的 LinkedList,检查这些对象是为了解析请求的属性。

PropertySourcesPlaceholderConfigurer 是一个具有自己状态的单独对象。它拥有自己的 MutablePropertySources 对象来解析属性占位符。 PropertySourcesPlaceholderConfigurer 实现 EnvironmentAware,因此当 ApplicationContext 获取它时,它会为其提供 Environment 对象。 PropertySourcesPlaceholderConfigurer 将此EnvironmentMutablePropertySources 添加到其自己的环境中。然后,它还会添加您使用 setLocation() 指定的各种 Resource 对象作为附加属性。 这些 Resource 对象不会添加到 EnvironmentMutablePropertySources 中,因此无法通过 env.getProperty(字符串)

因此,您无法将 PropertySourcesPlaceholderConfigurer 加载的属性直接获取到 Environment 中。您可以做的就是直接添加到 EnvironmentMutablePropertySouces 中。一种方法是使用

@PostConstruct
public void setup() throws IOException {
Resource resource = new FileSystemResource("spring.properties"); // your file
Properties result = new Properties();
PropertiesLoaderUtils.fillProperties(result, resource);
env.getPropertySources().addLast(new PropertiesPropertySource("custom", result));
}

或者简单地(感谢@M.Deinum)

@PostConstruct
public void setup() throws IOException {
env.getPropertySources().addLast(new ResourcePropertySource("custom", "file:spring.properties")); // the name 'custom' can come from anywhere
}

请注意,添加 @PropertySource 具有相同的效果,即。直接添加到环境,但您是静态而不是动态地执行此操作。

关于spring - PropertySourcesPlaceholderConfigurer 未在 SpringBoot 项目中注册环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21100729/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com