从0开始写JavaWeb框架系列(1)从0开始写SamrtFrameWork:读取配置文件

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(总结):至此数据库配置和路径配置已经读取完毕-------------------------------------

转载于:https://my.oschina.net/tianshuo/blog/678338

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值