在spring中加载配置文件的方式:
@Configuration @PropertySource({"classpath:config.properties"}) //@Import(DataSourceConfig.class) public class DefaultAppConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }
使用的地方注入Environemnt:
@Autowired private Environment env;
通过调用Environment的API来获得properties文件属性的值
PropertySource不能配置classpath*,所以要全局加载(类似于classpath*),使用:
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
先解析到资源,然后通过PropertySource进行解析,最后添加到Environment中去。
env.getPropertySources().addFirst(mps);
这样还是通过注入Environment的方式来统一获取到系统Properties文件的值