Spring读取properties文件

本文介绍了Spring中使用PropertyPlaceholderConfigurer和PropertiesFactoryBean来加载和使用properties文件的方法,包括从单个文件到多个文件的加载,以及如何通过注解和自定义配置类来获取配置值。此外,还提到了通过PropertiesLoaderUtils和Resource动态读取文件的方式,并讨论了getResourceAsStream的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设此时有properties文件:mytest.properties

testKey=testValue

1 利用PropertyPlaceholderConfigurer

1.1 注解

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

如果有多个文件的话:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
           <list>
               <value>mytest1.properties</value>
               <value>mytest2.properties</value>
           </list>
       </property>
</bean>

此时可以使用@Value直接使用:

@Value("${testKey}")
String configVal;

1.2 维护到容器中

或者继承PropertyPlaceholderConfigurer,将文件内容解析后放到一个容器中:

public class TestConfigurer extends PropertyPlaceholderConfigurer {

    private Map<String,Object> testPropMap;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        if(null == testPropMap){
            testPropMap= new HashMap<>();
        }
        for (Object key : props.keySet()){
            String keyStr = key.toString();
            Object value = props.get(key);
            testPropMap.put(keyStr,value);
        }
    }
    public List<String> getCtxProp(String name) {
        return encryptPropMap.get(name);
    }
}

<bean id="testConfigurer " class="com.test.TestConfigurer">
    <property name="location">
        <value>classpath:conf/mytest.properties</value>
    </property>
</bean>

此时就可以通过注入testConfigurer来获取了。

2 PropertiesFactoryBean

PropertiesFactoryBean使用上和PropertyPlaceholderConfigurer类似,仅仅是注解使用身上有些差异

<bean id="testConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:mytest.properties" />
</bean>

然后用注解:

@Value("#{$testConfig['testKey']}")
int configVal;

3 Properties与InputStream

此种方式可以动态的读取Properties文件,比如定义一个读取文件的类:

public class TestConfigurer {
	public static Properties getConfig(String configPath){
        InputStream inputStream = TestConfigurer .class.getResourceAsStream(configPath);
        if (inputStream == null) {
            return null;
        }
        try {
            Properties properties = new Properties();
            properties.load(inputStream);
            return properties;
        } catch (Exception e) {
            throw new RuntimeException("load config error", e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException ioe) {
                logger.error("close stream error", ioe);
            }
        }
    }
}

只要传入路径就能获取到Properties实例,也就获得了文件内容.
关于getResourceAsStream是用法,可以参考一下:Java中getResourceAsStream的用法这篇博文

4 PropertiesLoaderUtils与Resource

此种方式和类似,可以动态读取文件:

Resource resource = new ClassPathResource("/myTest.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);

路径

关于文件路径,如果路径以‘/’开头,则表示classpath目录下。

参考资料:
Spring获取properties文件中的属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yue_hu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值