Parameter Calibration and internationalization

Today we talk about the parameters checksum internationalization, the code is no technical content, but a lot of flooding in the business code, it is likely only a few lines of code business, there are a dozen lines of code check parameters, very much affect the code to read, so it is necessary to put this piece of code minus down.

Today's main purpose is to put the previous example inside and service independent international parameters hide, and how good package check function.

before fixing:

 

 

 service:

 

 

 Modified:

 

 

 

 

 

 Remove international parameters or the use of technology or ThreadLocal . International information can put away a few places, but it is not recommended to put on each url, in addition to relatively low also susceptible to many other problems. Demonstrated here is on the cookie example above:

 

 

 

public  class UserUtil {

    private final static ThreadLocal<String> tlUser = new ThreadLocal<String>();

    private final static  ThreadLocal<Locale> tlLocal = new ThreadLocal<Locale>(){
        protected Locale initialValue(){
            //语言的默认
            return Locale.CHINESE;
        }
    };

    public static final String KEY_LANG="lang";

    public static final String KEY_USER="user";


    public static void setUser(String userid){
        tlUser.set(userid);
        //把用户信息放到log4j
        MDC.put(KEY_USER,userid);
    }

    public static String getUser(){
        return tlUser.get();
    }

    public static void setLocale(String locale){
        setLocale(new Locale(locale));
    }

    public static void setLocale(Locale locale){
        tlLocal.set(locale);
    }

    public static Locale getLocale(){
        return tlLocal.get();
    }

    public static void clearAllUserInfo(){
        tlUser.remove();
        tlLocal.remove();
        MDC.remove(KEY_USER);
    }
}

CheckUtil

import com.example.demo.common.exception.CheckException;
import org.springframework.context.MessageSource;

public class CheckUtil {

    private static MessageSource resource;

    public static void setResource(MessageSource resource){
        CheckUtil.resource = resource;
    }

    public static void check(boolean condition,String msgKey,Object... args){
        if(!condition){
            fail(msgKey,args);
        }
    }

    public static void notEmpty(String str,String msgKey,Object... args){
        if(str==null || str.isEmpty()){
            fail(msgKey,args);
        }
    }

    public static  void notNull(Object obj,String msgKey,Object... args){
        if(obj==null){
            fail(msgKey,args);
        }
    }

    private static void fail(String msgKey,Object...args){
        throw new CheckException(resource.getMessage(msgKey,args,UserUtil.getLocale()));
    }
}

这里有几个小技术点:

工具类里面使用spring的bean,使用了MethodInvokingFactoryBean的静态方法注入:

<!-- 国际化 -->
<bean id="messageSource"
  class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
    <list>
      <value>format</value>
      <value>exceptions</value>
      <value>windows</value>
    </list>
  </property>
</bean>

<bean
  class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="staticMethod" value="plm.common.utils.CheckUtil.setResources" />
  <!-- 这里配置参数 -->
  <property name="arguments" ref="messageSource">
  </property>
</bean>

server里面调用的使用没有出现类名

 

 

这里使用的jdk的import static 特性,可以在ide上配置,请自行google。

import static plm.common.utils.CheckUtil.*;

还有一小点注意,我建议参数非法的时候,把值打印出来,否则你又要浪费时间看是没有传呢还是传错了,时间就是这样一点点浪费的。

check(id > 0L, "id.error", id); // 当前非法的id也传入提示出去

做了这几步之后,代码会漂亮很多,记住,代码最主要的不是性能,而是可读性,有了可读性才有才维护性。而去掉无关的代码后的代码,和之前的代码对比一下,自己看吧。另外有些项目用valid来校验,从我实际接触来看,用的不多,可能是有短木板吧。如果你的项目valid就能满足,那就更加好了,不需要看了。但是大部分场景,校验比例子复杂N多,提示也千变万化,所以我们还是自己调用函数校验。

还有人说代码要注释率到多少(我们公司有段时间工具扫描要求注释率到30%以上),依我看来,大部分业务代码这么简单,你把代码写成我例子那样,还需要什么注释?注释是画蛇添足。

Guess you like

Origin www.cnblogs.com/shareTechnologyl/p/11714438.html