env.getProperty not working Spring PropertyPlaceholderConfigurer

我只是一个虾纸丫 提交于 2019-12-01 06:55:12

A PropertyPlaceholderConfigurer does not add the properties from its locations to the Environment. With Java config, you can use @PropertySource to do that.

If any one want to achieve this without using @PropertySource

use ApplicationContextInitializer interface and its companion, the contextInitializerClasses servlet context param.

add this in web.xml

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>com.test.MyInitializer</param-value>
</context-param>

and define your Initializer

public class MyInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
    public void initialize(ConfigurableWebApplicationContext ctx) {
        PropertySource ps = new ResourcePropertySource(new ClassPathResource("sample.properties")); // handle exception
        ctx.getEnvironment().getPropertySources().addFirst(ps);
    }
}

Reference : Spring 3.1 M1: Unified Property Management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!