Architecture Adventure-learned knowledge

Source: Internet
Author: User

I. Commonly used tool classes

1. Tool classes for encoding and decoding

/*** Encoding and decoding Operation Tool class * *@authorSolverpeng * @create 2016-06-23-21:54*/ Public Final classCodecutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (codecutil.class); /*** Encode the URL*/     Public Staticstring Encodeurl (String source) {string target; Try{target= Urlencoder.encode (source, "UTF-8"); } Catch(unsupportedencodingexception e) {logger.error ("Encode URL failure", E); Throw NewRuntimeException (e); }        returnTarget; }    /*** Decode URL*/     Public Staticstring Decodeurl (String source) {string target; Try{target= Urldecoder.decode (source, "UTF-8"); } Catch(unsupportedencodingexception e) {logger.error ("Decode URL Failure", E); Throw NewRuntimeException (e); }        returnTarget; }}
Codecutil

Conversion between 2.POJO and JSON

/*** JSON Tool class * *@authorSolverpeng * @create 2016-06-24-11:54*/ Public Final classJsonutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (jsonutil.class); Private Static FinalObjectmapper Object_mapper =NewObjectmapper (); /*** Convert POJO to 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 NewRuntimeException (e); }        returnJSON; }    /*** Convert JSON to 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 NewRuntimeException (e); }        returnPojo; }}
Jsonutil

3. Reflection Tool Class

/*** Reflection Tool class * *@authorSolverpeng * @create 2016-06-23-18:06*/ Public Final classReflectionutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (reflectionutil.class); /*** Create an instance*/     Public StaticObject newinstance (class<?>CLS)        {Object instance; Try{instance=cls.newinstance (); } Catch(Exception e) {logger.error ("New instance Failure", E); Throw NewRuntimeException (e); }        returninstance; }    /*** Call Method*/     Public Staticobject 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 NewRuntimeException (e); }        returnresult; }    /*** Set Properties*/     Public Static voidSetField (Object obj, Field field, Object value) {Try{field.setaccessible (true);        Field.set (obj, value); } Catch(illegalaccessexception e) {logger.error ("Set field failure", E); Throw NewRuntimeException (e); }    }}
Reflectionutil

Two. Other knowledge

1. Annotation development

/*** Action Method Note * *@authorSolverpeng * @create 2016-06-23-16:58*/@Target (Elementtype.method) @Retention (retentionpolicy.runtime) Public@InterfaceAction {/*** Request type and path*/String value ();}/*** Controller Notes * *@authorSolverpeng * @create 2016-06-23-16:57*/@Target (Elementtype.type) @Retention (retentionpolicy.runtime) Public@InterfaceController {}/*** Dependency Injection annotations * *@authorSolverpeng * @create 2016-06-23-17:02*/@Target (Elementtype.field) @Retention (retentionpolicy.runtime) Public@InterfaceInject {}/*** Service Class NOTE * *@authorSolverpeng * @create 2016-06-23-17:00*/@Target (Elementtype.type) @Retention (retentionpolicy.runtime) Public@InterfaceService {}
Action Controller Service Inject

2. Handling of exceptions and logs

Private Static FinalLogger Logger = Loggerfactory.getlogger (codecutil.class); /*** Encode the URL*/     Public Staticstring Encodeurl (String source) {string target; Try{target= Urlencoder.encode (source, "UTF-8"); } Catch(unsupportedencodingexception e) {logger.error ("Encode URL failure", E); Throw NewRuntimeException (e); }        returnTarget; }
Logger

3. Definition of the configuration file

smart.framework.jdbc.driver=com.mysql.jdbc.Driversmart.framework.jdbc.url=jdbc:mysql://  Localhost:3306/demosmart.framework.jdbc.username=rootsmart.framework.jdbc.password=123456  Smart.framework.app.base_package=Com.nucsoft.chapter2smart.framework.app.jsp_path=/web-inf/view/ #表示静态资源文件的基础路径smart. Framework.app.asset_path=/asset/
smart.properties
/*** Provide the relevant configuration constant class * *@authorSolverpeng * @create 2016-06-23-14:31*/ Public Interfaceconfigconstant {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
/*** Properties File Helper class * *@authorSolverpeng * @create 2016-06-23-14:37*/ Public Final classConfigHelper {Private Static FinalProperties Confige_props =Propsutils.loadprops (Configconstant.config_file); /*** Get the JDBC driver*/     Public StaticString Getjdbcdriver () {returnpropsutils.getstring (Confige_props, configconstant.jdbc_driver); }    /*** Get JDBC URL*/     Public StaticString Getjdbcurl () {returnpropsutils.getstring (Confige_props, Configconstant.jdbc_url); }    /*** Get JDBC user name*/     Public StaticString Getjdbcusername () {returnpropsutils.getstring (Confige_props, configconstant.jdbc_username); }    /*** Get JDBC password*/     Public StaticString Getjdbcpassword () {returnpropsutils.getstring (Confige_props, Configconstant.jdbc_password); }    /*** Get app Base package name*/     Public StaticString getappbasepackage () {returnpropsutils.getstring (Confige_props, configconstant.app_base_package); }    /*** Get app JSP path*/     Public StaticString Getappjsppath () {returnPropsutils.getstring (Confige_props, Configconstant.app_jsp_path, "/web-inf/view/"); }    /*** Get application static resource path*/     Public StaticString Getappassetpath () {returnPropsutils.getstring (Confige_props, Configconstant.app_asset_path, "/asset/"); }}
ConfigHelper

Architecture Adventure-learned knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.