zl程序教程

您现在的位置是:首页 >  后端

当前栏目

spring 通过编程来获取属性文件

Spring文件属性编程 获取 通过
2023-09-11 14:21:41 时间

配置可以读取属性:

<beans profile="dev">
        <context:property-placeholder ignore-resource-not-found="true"
                                      location="classpath:/META-INF/config/dev/*.properties" />
    </beans>

    <beans profile="idc">
        <context:property-placeholder ignore-resource-not-found="true"
                                      location="classpath:/META-INF/config/idc/*.properties" />
    </beans>

    <beans profile="production">
        <context:property-placeholder ignore-resource-not-found="true"
                                      location="classpath:/META-INF/config/prod/*.properties" />
    </beans>

    <beans profile="test">
        <context:property-placeholder ignore-resource-not-found="true"
                                      location="classpath:/META-INF/config/test/*.properties" />
    </beans>

 

也可以通过编程的形式获取文件属性:

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

参考文献:

【1】http://stackoverflow.com/questions/1771166/access-properties-file-programatically-with-spring