配置文件名为:projectproperties,内容如下:
[html] view plain copy
# 是否开启逻辑删除
project_delfilteron=false
project_domain=
修改Spring配置文件
之前代码:
[html] view plain copy
<beanidbeanid="propertyConfigurer"
class="orgspringframeworkbeansfactoryconfigPropertyPlaceholderConfigurer">
<propertynamepropertyname="locations">
<list>
<value>classpath:dbinfoproperties</value>
</list>
</property>
</bean>
修改后的配置文件
[html] view plain copy
<beanidbeanid="propertyConfigurer"
class="comhisuncoreutilCustomizedPropertyPlaceholderConfigurer">
<propertynamepropertyname="locations">
<list>
<value>classpath:dbinfoproperties</value>
<value>classpath:projectproperties</value>
</list>
</property>
</bean>
加入了classpath:projectproperties,其为自定义的配置文件
将PropertyPlaceholderConfigurer类修改为自定义类CustomizedPropertyPlaceholderConfigurer,
PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍
注意下:这个configurer类获取的是所有properties的属性map,如果希望处理某个properties文件,需要在properties中
做一个命名区别,然后在加载的时候,根据key的前缀,进行获取。
定义CustomizedPropertyPlaceholderConfigurer类
类的具体内容为下,
[java] view plain copy
importjavautilHashMap;
importjavautilMap;
importjavautilProperties;
importorgspringframeworkbeansBeansException;
importorgspringframeworkbeansfactoryconfigConfigurableListableBeanFactory;
importorgspringframeworkbeansfactoryconfigPropertyPlaceholderConfigurer;
publicclass CustomizedPropertyPlaceholderConfigurer extendsPropertyPlaceholderConfigurer {
privatestatic Map ctxPropertiesMap;
@Override
protectedvoid processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props)throws BeansException {
superprocessProperties(beanFactoryToProcess, props);
ctxPropertiesMap =new HashMap();
for(Object key : propskeySet()) {
String keyStr = keytoString();
if(keyStrstartsWith("project_")){
String value = propsgetProperty(keyStr);
ctxPropertiesMapput(keyStr, value);
}
}
}
publicstatic Object getContextProperty(String name) {
returnctxPropertiesMapget(name);
}
}
定义获取配置文件中值的类SpringPropertiesUtil
类的具体内容如下:
[java] view plain copy
importorgspringframeworkbeansBeansException;
importorgspringframeworkcontextApplicationContext;
importorgspringframeworkcontextApplicationContextAware;
importorgspringframeworkstereotypeComponent;
/
Spring-PropertiesUtil工具类 -获取属性值
/
@Component
publicclass SpringPropertiesUtil implementsApplicationContextAware {
publicstatic final String KEY = "propertyConfigurer";
privatestatic ApplicationContext applicationContext;
publicvoid setApplicationContext(ApplicationContext applicationContext)
throwsBeansException {
SpringPropertiesUtilapplicationContext = applicationContext;
}
publicstatic ApplicationContext getApplicationContext() {
returnapplicationContext;
}
/
获取配置文件中的内容
@param keyName
@return
/
publicstatic String parseStr(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
getBean(KEY);
returncpgetContextProperty(keyName)toString();
}
/
获取配置文件中的内容
@param keyName
@return
/
publicstatic int parseInt(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
getBean(KEY);
returnIntegerparseInt(cpgetContextProperty(keyName)toString());
}
/
获取配置文件中的内容
@param keyName
@return
/
publicstatic double parseDouble(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
getBean(KEY);
returnDoubleparseDouble(cpgetContextProperty(keyName)toString());
}
}
这样,在项目当中就能够方便快速的获取properties文件中配置的参数
如SpringPropertiesUtilparseStr(“content”)
war3exe不是文件夹而是一个执行程序,一般在魔兽争霸游戏根目录下,先找找你把魔兽存在哪个盘,比如存在D盘,则打开顺序通常为我的电脑---D盘---魔兽争霸---war3exe;(在一些电脑里war3exe是木有图标滴··找找就行)
上下文是包含了一些在处理过程中遇到的一些信息,可以把它看成一个类或一个进行的全局变量,它是公共合作的。类似于一篇文章,整个文章可以叫上下文,是因可以从中获得所有可用到的信息。比如spring的上下文,你可以从中获取Sping的一些配置文件。比如你在java开发中,在父类中定义了一个map存储一些基本信息,那么在子类的继承中,你可以从中获得这些信息,那么这个map就可以理解为上下文。
以上就是关于如何在Spring容器中加载自定义的配置文件全部的内容,包括:如何在Spring容器中加载自定义的配置文件、如何获得 war中 配置文件的路径、在Java中什么是上下文(context)不懂的或者模模糊糊的网友别评论,谢谢。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)