• 手写web框架之加载配置项目


    一  定义框架配置项

    在项目的src/main/resources目录下创建一个名为smart.propertiesd的文件,文件的内容如下:

    1 smart.framework.jdbc.driver=com.mysql.jdbc.Driver
    2 smart.framework.jdbc.url=jdbc:mysql://localhost:3306/jack
    3 smart.framework.jdbc.username=root
    4 smart.framework.jdbc.password=root
    5  
    6 smart.framework.app.base_package=org.jack.smart4j
    7 smart.framework.app.jsp_path=/WEB_INF/view/
    8 smart.framework.app.asset_path=/asset/

    二   加载框架配置项
              既然有了配置文件,那么我们需要获取配置文件的值。这是框架需要做的事情,我们在smart-frame项目中创建一个ConfigHelper助手类,让它来读取smart.properties配置文件。
       首先,我们需要在smart-frame项目中创建一个名为ConfigConstant的常量类,让它来维护配置文件中的相关配置项名称,代码如下:

     1 package org.smart4j.framework;
     2  
     3 /**
     4  * Created by jack on 2017/5/22.
     5  * 提供相关常量配置项
     6  */
     7 public interface ConfigConstant {
     8     String CONFIG_FILE="smart.properties";
     9     String JDBC_DRIVER="smart.framework.jdbc.driver";
    10     String JDBC_URL="smart.framework.jdbc.url";
    11     String JDBC_USERNAME="smart.framework.jdbc.username";
    12     String JDBC_PASSWORD="smart.framework.jdbc.password";
    13  
    14     String APP_BASE_PACKAGE="smart.framework.app.base_package";
    15     String APP_JSP_PATH="smart.framework.app.jsp_path";
    16     String APP_ASSET_PATH="smart.framework.app.asset_path";
    17 }

    后我们需要借助PropUtil工具类来实现ConfigHelper,PropUtil的代码,中已经给出了在这里,我们在给出该代码

      1 package org.smart4j.framework.org.smart4j.framework.util;
      2  
      3 import org.slf4j.Logger;
      4 import org.slf4j.LoggerFactory;
      5 import java.io.FileNotFoundException;
      6 import java.io.IOException;
      7 import java.io.InputStream;
      8 import java.util.Properties;
      9  
     10 /**
     11  * Created by jack on 2015/12/26.
     12  * 属性文件工具类
     13  */
     14 public final class PropsUtil {
     15     private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);
     16  
     17     /*
     18     * 加载属性文件
     19     *
     20     * */
     21     public static Properties loadProps(String fileName) {
     22         Properties properties = null;
     23         InputStream inputStream = null;
     24         try {
     25             inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
     26             if (inputStream == null) {
     27                 throw new FileNotFoundException(fileName + " file is not found!");
     28             }
     29             properties = new Properties();
     30             properties.load(inputStream);
     31         } catch (IOException e) {
     32             LOGGER.error("load properties file failure", e);
     33         } finally {
     34             if (inputStream != null) {
     35                 try {
     36                     inputStream.close();
     37                 } catch (IOException e) {
     38                     LOGGER.error("close input stream failure", e);
     39                 }
     40             }
     41         }
     42         return properties;
     43     }
     44  
     45     /*
     46     * 获取字符型属性(默认为空字符串)
     47     *
     48     * */
     49     public static String getString(Properties props, String key) {
     50         return getString(props, key, "");
     51     }
     52  
     53     /*
     54     * 获取字符型属性(可指定默认值)
     55     * */
     56     public static String getString(Properties props, String key, String
     57             defaultValue) {
     58         String value = defaultValue;
     59         if (props.containsKey(key)) {
     60             value = props.getProperty(key);
     61         }
     62         return value;
     63     }
     64  
     65     /*
     66     * 获取数值类型属性(默认为0)
     67     * */
     68     public static int getInt(Properties props, String key) {
     69         return getInt(props, key, 0);
     70     }
     71  
     72     /*
     73     * 获取数值类型属性(可指定默认值)
     74     * */
     75     public static int getInt(Properties props, String key, int defaultValue) {
     76         int value = defaultValue;
     77         if (props.containsKey(key)) {
     78             value = CastUtil.castInt(props.getProperty(key));
     79         }
     80         return value;
     81     }
     82  
     83     /*
     84     * 获取布尔型属性(默认值为false)
     85     * */
     86     public static boolean getBoolean(Properties props, String key) {
     87         return getBoolean(props, key, false);
     88     }
     89  
     90     /*
     91     * 获取布尔型属性(可指定默认值)
     92     * */
     93     public static boolean getBoolean(Properties props, String key, Boolean defaultValue) {
     94         boolean value = defaultValue;
     95         if (props.containsKey(key)) {
     96             value = CastUtil.castBoolean(props.getProperty(key));
     97         }
     98         return value;
     99     }
    100 }

    我们借助上面的帮助类,就可以获取smart.properties配置文件中的项,代码如下:

     1 package org.smart4j.framework.helper;
     2  
     3 import org.smart4j.framework.ConfigConstant;
     4 import org.smart4j.framework.org.smart4j.framework.util.PropsUtil;
     5  
     6 import java.util.Properties;
     7  
     8 /**
     9  * Created by jack on 2017/5/22.
    10  * 属性文件助手类
    11  */
    12 public class ConfigHelper {
    13     private static final Properties CONFIG_PROPS = PropsUtil.loadProps(ConfigConstant.CONFIG_FILE);
    14     /**
    15      * 获取JDBC驱动
    16      */
    17     public static String getJdbcDriver(){
    18         return PropsUtil.getString(CONFIG_PROPS, ConfigConstant.JDBC_DRIVER);
    19     }
    20     /**
    21      * 获取JDBC URL
    22      */
    23     public static String getJdbcUrl(){
    24         return PropsUtil.getString(CONFIG_PROPS,ConfigConstant.JDBC_URL);
    25     }
    26     /**
    27      * 获取JDBC 用户名
    28      */
    29     public static String getJdbcUsername(){
    30         return PropsUtil.getString(CONFIG_PROPS,ConfigConstant.JDBC_USERNAME);
    31     }
    32     /**
    33      * 获取JDBC 密码
    34      */
    35     public static String getJdbcPassword(){
    36         return PropsUtil.getString(CONFIG_PROPS,ConfigConstant.JDBC_PASSWORD);
    37     }
    38     /**
    39      * 获取应用基础包名
    40      */
    41     public static String getAppBasePackage(){
    42         return PropsUtil.getString(CONFIG_PROPS,ConfigConstant.APP_BASE_PACKAGE);
    43     }
    44     /**
    45      * 获取应用jsp路径
    46      */
    47     public static String getAppJspPath(){
    48         return PropsUtil.getString(CONFIG_PROPS,ConfigConstant.APP_JSP_PATH,"/WEB-INF/view");
    49     }
    50     /**
    51      * 获取应用静态资源路径
    52      */
    53     public static String getAppAssetPath(){
    54         return PropsUtil.getString(CONFIG_PROPS,ConfigConstant.APP_ASSET_PATH,"/asset");
    55     }
    56 }
  • 相关阅读:
    《那些年啊,那些事——一个程序员的奋斗史》——81
    《那些年啊,那些事——一个程序员的奋斗史》——83
    《那些年啊,那些事——一个程序员的奋斗史》——80
    《那些年啊,那些事——一个程序员的奋斗史》——82
    《那些年啊,那些事——一个程序员的奋斗史》——81
    《那些年啊,那些事——一个程序员的奋斗史》——82
    《那些年啊,那些事——一个程序员的奋斗史》——82
    网络学习杂七杂八
    字典类的代码的学习
    SNMP++ 编译记录
  • 原文地址:https://www.cnblogs.com/duan2/p/11747746.html
Copyright © 2020-2023  润新知