在上一篇博客中,我们说了PropertyPlaceholderConfigurer类的作用,使用该类方便我们对配置文件的管理,然后我们在代码中如何获取属性文件的值呢?
既然PropertyPlaceholderConfigurer类可以加载properties文件,那自然我们可以自定义一个它的子类,在不影响它原来功能的情况下,存储我们需要的内容。
public MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
private static Map myProperties;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) throws BeansException{
super.processProerties(beanFactoryToProcess,props);
myProperties=new HashMap();
for(Object key:props.keySet()){
String keySet = key.toString();
String value=props.getProperty(keySet);
myProperties.put(keySet,value);
}
}
public static Object getContextProperty(String name){
return myProperties.get(name);
}
}
有了这个类,我们就在BeanFactory中将原来的PropertyPlaceholderConfigurer类换成该类
<bean id="propertyConfigurer" class="MyPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
<list>
</property>
</bean>
至此我们实现了在不影响原来的框架的情况下将属性文件的内容放到了myProperties中一份。
但是MyPropertyPlaceholderConfigurer作为一个bean是存在于容器中的,因此我们需要获得容器的上下文环境。
//@Component(或者在BeanFactory中配置)
public class MyPropertiesUtil implements ApplicationContextAware{
public static final String KEY="propertyConfigurer";
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(Application applicationContext) throws BeansExcepton{
MyPropertiesUtil.applicationContext=applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
public static String getValue(String key){
MyPropertyPlaceholderConfigurer cp=(MyPropertyPlaceholderConfigurer)applicationContext.getBean(KEY);
return cp.getApplicationContext(key).toString();
}
}
至此我们已经写好了可以在代码中使用的Util类,但是无缘无故框架不会加载你自定义的类
因此我们需要加@Component注释或者在BeanFactory中加入
<bean class="MyPropertiesUtil"/>
自此,我们可以使用MyPropertiesUtil.getValue(key)来获得自定义文件中的内容,但是注意:
这段代码不能在bean的属性中使用,比如某个bean中存在代码,定义属性private int pagesize=MyPropertiesUtil.getValue(key);这段代码会有时候对有时候错的情况,因为这两个bean(pagesize属性所在的类bean依赖myPropertiesUtil类的bean,但实际上myPropertiesUtil中ApplicationContext的注入是在所有bean创建之后的,所以会出现问题。)
分享到:
相关推荐
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
NULL 博文链接:https://konghaibo.iteye.com/blog/430146
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
我们使用Spring Boot编写多个模块开发时,我们希望各个模块使用各自的配置文件,避免将整个项目的配置文件都写在一起,从而降低各个模块之间的耦合度。
Spring Boot读取配置文件常用方式
spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,
NULL 博文链接:https://jiachenmianyu.iteye.com/blog/1745042
主要介绍了Spring读取配置文件属性实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
此工具自动生成非注解时Spring整合MyBatis时需要的一些配置文件,对于使用较新版本的spring,可以忽视生成的dao的配置文件,建议采用spring的扫描方式。生成的GeneratorConfigXML修改jdbc驱动位置后可直接用使用...
Spring源码分析:配置文件读取流程 - ImportNew
spring读取properties配置文件的key值。
NULL 博文链接:https://songwensheng.iteye.com/blog/629578
本资源对spring-boot中的核心配置文件进行详细介绍,具体内容参考博客:http://blog.csdn.net/pengjunlee/article/details/79640361,包括:核心配置文件介绍,读取配置,动态加载配置等。
NULL 博文链接:https://java--hhf.iteye.com/blog/2171034
Spring Cloud配置中心获取不到最新配置信息的问题
本demo springCloud版本为1.5.9,...本项目使用本地分布式配置(非git远程配置),启动顺序为服务注册中心(discovery)、配置中心(confugure)、微服务(micro-server),配置文件ip需修改为你自己的ip或localhost。
Spring配置文件中注册三种数据源及从属性文件中读取DB连接四要素
实践方志鹏博客搭建Springcloud+RabbitMQ+Config-client+config-server Eureka-server的微服务架构,通过/bus/refresh接口触发所有config-client自动从config-server重新读取配置文件。SpringCloud和SpringBoot版本...
主要介绍了详解Spring Boot读取配置文件与配置文件优先级,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring Boot技术知识点:如何获取application.yml配置文件里的相关属性