SmartFramework
一、读取配置文件
1.加入第三方开源jar
2.编写smart.properties配置文件( 数据库连接和路径的配置 base_package:基础包名,/WEB-INF/view/:jsp基础路径,asset_path:静态文件基础路径 )
3.加载smart.properties配置项到内存
3.1 ConfigConstant.java提供Properties文件的key相关配置常量
package org.smart4j.framework;
/**
* 提供相关配置项常量
* @author tianshuo
* @since 1.1
*/
public interface ConfigConstant {
String CONFIG_FILE = "smart.properties";
String JDBC_DRIVER ="smart.framework.jdbc.driver";
String JDBC_URL ="smart.framework.jdbc.url";
String JDBC_USERNAME = "smart.framework.jdbc.username";
String JDBC_PASSWORD="smart.framework.jdbc.password";
String APP_BASE_PACKAGE="smart.framework.app.base_package";
String APP_JSP_PATH="smart.framework.app.jsp_path";
String APP_ASSET_PATH="smart.framework.app.asset_path";
}
3.2 PropsUtil.java简化Properties文件读取的工具类
package org.smart4j.framework.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Properties文件读取工具类
* @author Admin
*
*/
public class PropsUtil {
/**
* 加载properties配置文件工具类
* @param fileConfig
* @return
*/
public static Properties loadProps(String fileConfig){
Properties properties = new Properties();
try {
InputStream in = Object.class.getResourceAsStream("/" + fileConfig);
properties.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return properties;
}
/**
* 根据传入的properties文件对象的key获得value
* @param properties
* @param key
* @return value
*/
public static String getString(Properties properties,String key){
String value = properties.getProperty(key);
return value;
}
/**
* 根据传入的properties文件对象的key获得value,提供可选的路径配置项pathCustom
* @param properties
* @param key
* @param pathCustom 自定义配置项,传入null默认加载配置文件key
* @return value
*/
public static String getString(Properties properties,String key,String pathCustom){
if( pathCustom != null ){
return pathCustom;
}else{
String value = properties.getProperty(key);
return value;
}
}
}
3.3 ConfigHelper.java读取Properties文件的Value
package org.smart4j.framework.helper;
import java.util.Properties;
import org.smart4j.framework.ConfigConstant;
import org.smart4j.framework.util.PropsUtil;
/**
* 属性文件助手类
* @author
*
*/
public final class ConfigHelper {
private static final Properties CONFIG_PROPS = PropsUtil.loadProps(ConfigConstant.CONFIG_FILE);
/**
* 获取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/");
}
//Test
public static void main(String[] args) {
System.out.println(ConfigHelper.getJdbcUsername());
System.out.println(ConfigHelper.getJdbcPassword());
System.out.println(ConfigHelper.getAppBasePackage());
System.out.println(ConfigHelper.getAppJspPath());
System.out.println(ConfigHelper.getAppAssetPath());
}
}
---------------------------------------summarize(总结):至此数据库配置和路径配置已经读取完毕-------------------------------------