java中读取服务器配置文件方法
来源:互联网 发布:exescope软件下载 编辑:程序博客网 时间:2024/05/30 23:02
自定义配置文件
配置文件名为:project.properties,内容如下:
1
2
3
# 是否开启逻辑删除
del.filter.on=
false
domain=http:
//www.366go.cn/
修改Spring配置文件
之前代码:
1
2
3
4
5
6
7
8
<
bean
id
=
"propertyConfigurer"
class
=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<
property
name
=
"locations"
>
<
list
>
<
value
>classpath:dbinfo.properties</
value
>
</
list
>
</
property
>
</
bean
>
修改后的配置文件
1
2
3
4
5
6
7
8
9
<
bean
id
=
"propertyConfigurer"
class
=
"com.hisun.core.util.CustomizedPropertyPlaceholderConfigurer"
>
<
property
name
=
"locations"
>
<
list
>
<
value
>classpath:dbinfo.properties</
value
>
<
value
>classpath:project.properties</
value
>
</
list
>
</
property
>
</
bean
>
加入了classpath:project.properties,其为自定义的配置文件
将PropertyPlaceholderConfigurer类修改为自定义类CustomizedPropertyPlaceholderConfigurer,
PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍
定义CustomizedPropertyPlaceholderConfigurer类
类的具体内容为下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import
java.util.HashMap;
import
java.util.Map;
import
java.util.Properties;
import
org.springframework.beans.BeansException;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public
class
CustomizedPropertyPlaceholderConfigurer
extends
PropertyPlaceholderConfigurer {
private
static
Map ctxPropertiesMap;
@Override
protected
void
processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props)
throws
BeansException {
super
.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap =
new
HashMap();
for
(Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public
static
Object getContextProperty(String name) {
return
ctxPropertiesMap.get(name);
}
}
定义获取配置文件中值的类SpringPropertiesUtil
类的具体内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import
org.springframework.beans.BeansException;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.ApplicationContextAware;
import
org.springframework.stereotype.Component;
/**
* Spring-PropertiesUtil工具类 -获取属性值
*
*/
@Component
public
class
SpringPropertiesUtil
implements
ApplicationContextAware {
public
static
final
String KEY =
"propertyConfigurer"
;
private
static
ApplicationContext applicationContext;
public
void
setApplicationContext(ApplicationContext applicationContext)
throws
BeansException {
SpringPropertiesUtil.applicationContext = applicationContext;
}
public
static
ApplicationContext getApplicationContext() {
return
applicationContext;
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public
static
String parseStr(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return
cp.getContextProperty(keyName).toString();
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public
static
int
parseInt(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return
Integer.parseInt(cp.getContextProperty(keyName).toString());
}
/**
* 获取配置文件中的内容
*
* @param keyName
* @return
*/
public
static
double
parseDouble(String keyName) {
CustomizedPropertyPlaceholderConfigurer cp = (CustomizedPropertyPlaceholderConfigurer) applicationContext
.getBean(KEY);
return
Double.parseDouble(cp.getContextProperty(keyName).toString());
}
}
这样,在项目当中就能够方便快速的获取properties文件中配置的参数
如SpringPropertiesUtil.parseStr(“content”)
0 0
- java中读取服务器配置文件方法
- java服务器读取配置文件
- java中读取配置文件的方法
- java中读取配置文件的方法
- java中读取配置文件的方法
- java中读取配置文件的方法
- java中读取配置文件的方法
- java读取配置文件方法
- java中读取配置文件
- java中读取配置文件
- java中读取配置文件
- java中读取配置文件中数据的具体方法
- java中读取配置文件信息的六种方法
- java中读取配置文件信息的六种方法
- java读取项目中配置文件的几种方法
- Java中spring读取配置文件的几种方法
- java中读取配置文件的方法fileSystemXmlapplicationContext与classPathXmlApplicationContext
- java读取配置文件方法大全
- Mac下为Android项目添加Maven
- 时间规划
- 怎么加载包含文件
- 头文件和库的理解
- 【Unity快速实现小功能】NGUI篇——快速实现一个可滑动列表
- java中读取服务器配置文件方法
- 12-26-2014——复习java基础——星期五
- 运行.exe程序时,提示缺少MFC42
- Java程序通过weka调用libsvm的方法
- Java学习 泛型
- 数据结构第六章知识总结
- 工作态度
- 程序员如何编写出更有保存价值和学习价值的程序
- SQL Server LocalDB 在 ASP.NET中的应用