小编典典

使用Spring以编程方式访问属性文件?

spring

我们使用下面的代码从属性文件中注入具有属性的Spring bean。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:/my.properties"/>
</bean>

<bean id="blah" class="abc">
    <property name="path" value="${the.path}"/>
</bean>

有没有一种方法可以通过编程方式访问属性?我试图做一些没有依赖注入的代码。所以我只想要一些这样的代码:

PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
props.load("classpath:/my.properties");
props.get("path");

阅读 438

收藏
2020-04-13

共1个答案

小编典典

怎么样PropertiesLoaderUtils

Resource resource = new ClassPathResource("/my.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
2020-04-13