动手写web框架(1): 加载配置项

定义框架所需的配置项 smart.properties

在src/main/resources目录下创建名为smart.properties的文件。

1
2
3
4
5
6
7
8
9
10
11
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
# 应用基础包名
app.base_package=com.roger.demo
# 应用jsp路径
app.jsp_path=/WEB-INF/view
# 应用静态资源路径
app.asset_path=/asset/

加载配置项

ConfigHelper
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
60
61
62
63
64
65
66
67
68
69
70
package org.smart4j.framework.helper;
import org.smart4j.framework.util.ConfigConstant;
import org.smart4j.framework.util.PropsUtil;
import java.util.Properties;
/**
* Created by Roger on 2016/11/24.
*/
public class ConfigHelper {
private static final Properties CONFIG_PROPS = PropsUtil.loadProps(ConfigConstant.CONFIG_FILE);
/**
* 获取JDBC驱动配置
* @return
*/
public static String getJdbcDriver(){
return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.JDBC_DRIVER);
}
/**
* 获取JDBC URL
* @return
*/
public static String getJdbcUrl(){
return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.JDBC_URL);
}
/**
* 获取JDBC 用户名
* @return
*/
public static String getJdbcUsername(){
return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.JDBC_USERNAME);
}
/**
* 获取JDBC 密码
* @return
*/
public static String getJdbcPassword(){
return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.JDBC_PASSWORD);
}
/**
* 获取应用基础包名
* @return
*/
public static String getAppBasePackage(){
return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.APP_BASE_PACKAGE);
}
/**
* 获取应用jsp路径
* @return
*/
public static String getAppJspPath(){
return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.APP_JSP_PATH, "/WEB-INF/view/");
}
/**
* 获取应用静态资源路径
* @return
*/
public static String getAppAssetPath(){
return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.APP_ASSET_PATH, "/asset/");
}
}
ConfigConstant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package org.smart4j.framework.util;
/**
* 提供相关配置项常量
* Created by Roger on 2016/11/23.
*/
public interface ConfigConstant {
String CONFIG_FILE = "smart.properties";
String JDBC_DRIVER = "jdbc.driver";
String JDBC_URL = "jdbc.url";
String JDBC_USERNAME = "jdbc.username";
String JDBC_PASSWORD = "jdbc.password";
String APP_BASE_PACKAGE = "app.base_package";
String APP_JSP_PATH = "app.jsp_path";
String APP_ASSET_PATH = "app.asset_path";
}
PropsUtil
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.smart4j.framework.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Created by Roger on 2016/11/23.
*/
public final class PropsUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);
/**
* 加载属性文件
*
* @param fileName
* @return
* @throws FileNotFoundException
*/
public static Properties loadProps(String fileName) {
Properties props = null;
InputStream is = null;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
if (is == null) {
throw new FileNotFoundException(fileName + " file is not found");
}
props = new Properties();
props.load(is);
} catch (IOException e) {
LOGGER.error("load properties '?' file failure", fileName, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
LOGGER.error("close input stream failure", e);
}
}
}
return props;
}
/**
* 获取String类型属性(默认值为空字符串)
*
* @param props
* @param key
* @return
*/
public static String getString(Properties props, String key) {
return getString(props, key, "");
}
/**
* 获取String类型属性(可以设置默认值)
*
* @param props
* @param key
* @param defaultValue
* @return
*/
public static String getString(Properties props, String key, String defaultValue) {
String value = defaultValue;
if (props.containsKey(key)) {
value = props.getProperty(key);
}
return value;
}
/**
* 获取int类型属性(默认值为0)
*
* @param props
* @param key
* @return
*/
public static int getInt(Properties props, String key) {
return getInt(props, key, 0);
}
/**
* 获取int类型属性(可以设置默认值)
*
* @param props
* @param key
* @param defaultValue
* @return
*/
public static int getInt(Properties props, String key, int defaultValue) {
int value = defaultValue;
if (props.containsKey(key)) {
value = Integer.parseInt(props.getProperty(key));
}
return value;
}
/**
* 获取boolean类型属性(默认值为false)
*
* @param props
* @param key
* @return
*/
public static boolean getBoolean(Properties props, String key) {
return getBoolean(props, key, false);
}
/**
* 获取boolean类型属性(可以设置默认值)
*
* @param props
* @param key
* @param defaultValue
* @return
*/
public static boolean getBoolean(Properties props, String key, boolean defaultValue) {
boolean value = defaultValue;
if (props.containsKey(key)) {
value = Boolean.parseBoolean(props.getProperty(key));
}
return value;
}
}