gpt4 book ai didi

spring - 如何在spring bean中注入(inject)完整的属性文件

转载 作者:IT老高 更新时间:2023-10-28 13:47:42 25 4
gpt4 key购买 nike

我有一个包含很多值的属性文件,我不想在我的 bean 配置文件中单独列出它们。例如:

<property name="foo">
<value>${foo}</value>
</property>
<property name="bar">
<value>${bar}</value>
</property>

等等。

我想将所有内容完全注入(inject)为 java.util.Properties 或更少作为 java.util.Map。有办法吗?

最佳答案

对于 Java 配置,您可以使用如下内容:

@Autowired @Qualifier("myProperties")
private Properties myProps;

@Bean(name="myProperties")
public Properties getMyProperties() throws IOException {
return PropertiesLoaderUtils.loadProperties(
new ClassPathResource("/myProperties.properties"));
}

如果您为每个实例分配一个唯一的 bean 名称(Qualifier),您也可以通过这种方式拥有多个属性。

关于spring - 如何在spring bean中注入(inject)完整的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5938768/

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