zoukankan      html  css  js  c++  java
  • 架构探险——学到的知识

    一.常用到的工具类

    1.用于编码和解码的工具类

    /**
     * 编码与解码操作工具类
     *
     * @author solverpeng
     * @create 2016-06-23-21:54
     */
    public final class CodecUtil {
        private static final Logger LOGGER = LoggerFactory.getLogger(CodecUtil.class);
    
        /**
         * 将 URL 编码
         */
        public static String encodeURL(String source) {
            String target;
            try {
                target = URLEncoder.encode(source, "UTF-8");
            } catch(UnsupportedEncodingException e) {
                LOGGER.error("encode url failure", e);
                throw new RuntimeException(e);
            }
            return target;
        }
    
        /**
         * 将URL解码
         */
        public static String decodeURL(String source) {
            String target;
    
            try {
                target = URLDecoder.decode(source, "UTF-8");
            } catch(UnsupportedEncodingException e) {
                LOGGER.error("decode url failure", e);
                throw new RuntimeException(e);
            }
    
            return target;
        }
    }
    CodecUtil

    2.POJO 和 JSON 之间的相互转换

    /**
     * JSON工具类
     *
     * @author solverpeng
     * @create 2016-06-24-11:54
     */
    public final class JsonUtil {
        private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
    
        private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    
        /**
         * 将 POJO 转换为 JSON
         */
        public static <T> String toJson(T obj) {
            String json;
    
            try {
                json = OBJECT_MAPPER.writeValueAsString(obj);
            } catch(JsonProcessingException e) {
                LOGGER.error("convert POJO to JSON failure", e);
                throw new RuntimeException(e);
            }
    
            return json;
        }
    
        /**
         * 将 JSON 转换为 POJO
         */
        public static <T> T fromJson(String json, Class<T> type) {
            T pojo;
            try {
                pojo = OBJECT_MAPPER.readValue(json, type);
            } catch(IOException e) {
                LOGGER.error("convert JSON to POJO failure", e);
                throw new RuntimeException(e);
            }
            return pojo;
        }
    
    }
    JsonUtil

    3.反射工具类

    /**
     * 反射工具类
     *
     * @author solverpeng
     * @create 2016-06-23-18:06
     */
    public final class ReflectionUtil {
        private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class);
    
        /**
         * 创建实例
         */
        public static Object newInstance(Class<?> cls) {
            Object instance;
    
            try {
                instance = cls.newInstance();
            } catch(Exception e) {
                LOGGER.error("new instance failure", e);
                throw new RuntimeException(e);
            }
    
            return instance;
        }
    
        /**
         * 调用方法
         */
        public static Object invokeMethod(Object obj, Method method, Object... args) {
            Object result;
    
            try {
                method.setAccessible(true);
                result = method.invoke(obj, args);
            } catch(Exception e) {
                LOGGER.error("invoke method failure", e);
                throw new RuntimeException(e);
            }
            return result;
        }
    
        /**
         * 设置属性
         */
        public static void setField(Object obj, Field field, Object value) {
            try {
                field.setAccessible(true);
                field.set(obj, value);
            } catch(IllegalAccessException e) {
                LOGGER.error("set field failure", e);
                throw new RuntimeException(e);
            }
    
        }
    
    }
    ReflectionUtil

    二.其他的知识

    1.注解开发

    /**
     * Action方法注解
     *
     * @author solverpeng
     * @create 2016-06-23-16:58
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Action {
        /**
         * 请求类型与路径
         */
        String value();
    }
    
    /**
     * 控制器注解
     *
     * @author solverpeng
     * @create 2016-06-23-16:57
     */
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Controller {}
    
    /**
     * 依赖注入注解
     *
     * @author solverpeng
     * @create 2016-06-23-17:02
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Inject {}
    
    /**
     * 服务类注解
     *
     * @author solverpeng
     * @create 2016-06-23-17:00
     */
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Service {}
    Action Controller Service Inject

    2.异常和日志的处理

    private static final Logger LOGGER = LoggerFactory.getLogger(CodecUtil.class);
    
        /**
         * 将 URL 编码
         */
        public static String encodeURL(String source) {
            String target;
            try {
                target = URLEncoder.encode(source, "UTF-8");
            } catch(UnsupportedEncodingException e) {
                LOGGER.error("encode url failure", e);
                throw new RuntimeException(e);
            }
            return target;
        }
    Logger

    3.配置文件的定义

    smart.framework.jdbc.driver=com.mysql.jdbc.Driver
    smart.framework.jdbc.url=jdbc:mysql://localhost:3306/demo
    smart.framework.jdbc.username=root
    smart.framework.jdbc.password=123456
    
    smart.framework.app.base_package=com.nucsoft.chapter2
    smart.framework.app.jsp_path=/WEB-INF/view/
    #表示静态资源文件的基础路径
    smart.framework.app.asset_path=/asset/
    smart.properties
    /**
     * 提供相关配置常量类
     *
     * @author solverpeng
     * @create 2016-06-23-14:31
     */
    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";
    }
    ConfigConstant
    /**
     * 属性文件助手类
     *
     * @author solverpeng
     * @create 2016-06-23-14:37
     */
    public final class ConfigHelper {
        private static final Properties CONFIGE_PROPS = PropsUtils.loadProps(ConfigConstant.CONFIG_FILE);
    
        /**
         * 获取 JDBC 驱动
         */
        public static String getJdbcDriver() {
            return PropsUtils.getString(CONFIGE_PROPS, ConfigConstant.JDBC_DRIVER);
        }
    
        /**
         * 获取 JDBC URL
         */
        public static String getJdbcUrl() {
            return PropsUtils.getString(CONFIGE_PROPS, ConfigConstant.JDBC_URL);
        }
    
        /**
         * 获取 JDBC 用户名
         */
        public static String getJdbcUsername() {
            return PropsUtils.getString(CONFIGE_PROPS, ConfigConstant.JDBC_USERNAME);
        }
    
        /**
         * 获取 JDBC 密码
         */
        public static String getJdbcPassword() {
            return PropsUtils.getString(CONFIGE_PROPS, ConfigConstant.JDBC_PASSWORD);
        }
    
        /**
         * 获取应用基础包名
         */
        public static String getAppBasePackage() {
            return PropsUtils.getString(CONFIGE_PROPS, ConfigConstant.APP_BASE_PACKAGE);
        }
    
        /**
         * 获取应用 JSP 路径
         */
        public static String getAppJspPath() {
            return PropsUtils.getString(CONFIGE_PROPS, ConfigConstant.APP_JSP_PATH, "/WEB-INF/view/");
        }
    
        /**
         * 获取应用静态资源路径
         */
        public static String getAppAssetPath() {
            return PropsUtils.getString(CONFIGE_PROPS, ConfigConstant.APP_ASSET_PATH, "/asset/");
        }
    
    }
    ConfigHelper
  • 相关阅读:
    个人工作总结2
    个人工作总结1
    《梦断代码》阅读笔记01
    构建之法阅读笔记4
    四则运算-安卓版
    最大联通子数组之和(dfs,记忆化搜索,状态压缩)
    第五周进度报告
    第四周进度报告
    最大子数列之和问题
    构建之法阅读笔记3
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5617922.html
Copyright © 2011-2022 走看看