【優雅程式碼】07-spring下的優秀工具類

Seal_lee發表於2022-01-05

【優雅程式碼】07-spring下的優秀工具類

歡迎關注b站賬號/公眾號【六邊形戰士夏寧】,一個要把各項指標拉滿的男人。該文章已在github目錄收錄。
螢幕前的大帥比大漂亮如果有幫助到你的話請順手點個贊、加個收藏這對我真的很重要。別下次一定了,都不關注上哪下次一定。

1.反射相關(重要)

1.1背景

ReflectionUtils和AnnotationUtils,各種姿勢進行反射,最關鍵是不用拋異常出去,相當舒心

1.2使用

@Nullable
private static void reflectionExample() {
    // 各種反射姿勢,可以躲過程式碼檢查
    Method reflectionExample = ReflectionUtils.findMethod(SpringExample.class, "reflectionExample");
    System.out.println(reflectionExample);
    Field testName = ReflectionUtils.findField(SpringExample.class, "testName");
    ReflectionUtils.makeAccessible(testName);
    System.out.println(testName);
    Nullable annotation = AnnotationUtils.findAnnotation(reflectionExample, Nullable.class);
    System.out.println(annotation);
}

2.cglib相關(重要)

2.1背景

cglib作為直接生成位元組碼,速度上不言而喻,而作為spring引入不用額外引入再次加分,以下介紹個人覺得用起來非常舒服的姿勢(代理模式不介紹,直接用springAop即可)

2.2使用

/private static void cglibExample() {
    // 注意cglib是對位元組碼操作,代理模式就不在這裡介紹了,spring aop非常好用了,不過這個是spring帶的cglib實際上不是spring的東西

    // 建立不可變bean,簡直太好用了,避免快取被別人瞎改
    SpringExample bean = new SpringExample();
    bean.setTestName("hello");
    SpringExample immutableBean = (SpringExample) ImmutableBean.create(bean);
    // 下面這步會直接報錯
    // immutableBean.setTestName("123");

    // 物件複製,目前最快的複製,第一個source,第二個target,如果要複製list需要自行迴圈
    BeanCopier copier = BeanCopier.create(SpringExample.class, SpringExample.class, false);
    SpringExample sourceBean = new SpringExample();
    SpringExample targetBean = new SpringExample();
    sourceBean.setTestName("123");
    targetBean.setTestName("223");
    copier.copy(sourceBean, targetBean, null);
    System.out.println(targetBean);
    // 注意第一步可以static快取起來,BulkBean雖然可以處理複雜邏輯,但是個人認為複雜邏輯就老實寫程式碼實現,用這個反而累贅

    // 物件轉map,可以重新封裝,也可以直接用
    Map<String, Object> map = new HashMap<>();
    map.putAll(BeanMap.create(targetBean));
    Map<String, Object> beanMap = BeanMap.create(targetBean);
    System.out.println(map);
    System.out.println(beanMap);

    // map轉物件
    SpringExample springExampleFinal = new SpringExample();
    BeanMap.create(springExampleFinal).putAll(map);
    System.out.println(springExampleFinal);
}

3.spring相關(重要)

3.1背景

列了4個比較好用的工具類,request非常棒,擴充套件的話可以在任意位置獲取到使用者,StopWatch在流程繁雜的方法中可以直觀輸出速度消耗的百分比,花板子,但是我很喜歡

3.2使用

private static void springExample() {
    // 獲取request
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    // 獲取cookie
    Cookie cookie = WebUtils.getCookie(request, "hello");
    // 轉義url
    UriUtils.decode("", StandardCharsets.UTF_8);
    UriUtils.encode("", StandardCharsets.UTF_8);
    // 記錄時間戳
    StopWatch sw = new StopWatch("startTest");
    sw.start("step 1");
    sw.stop();
    sw.start("step 2");
    sw.stop();
    System.out.println(sw.prettyPrint());
}

4.bean相關(重要)

4.1背景

列了3個使用姿勢,應該是使用頻率最高的3個

4.2使用

private void beanExample(){
    // 獲取bean
    SpringExample bean = ac.getBean(SpringExample.class);
    // 根據繼承或實現獲取bean
    Map<String, SpringExample> beansOfType = ac.getBeansOfType(SpringExample.class);
    // 獲取當前代理物件,service層常用
    AopContext.currentProxy();
}

5.assert相關

5.1背景

斷言工具,可以直接拋異常,不用寫trycatch,節省程式碼

5.2使用

private static void assertExample() {
    Assert.notEmpty(new ArrayList<>());
}

6.其它

6.1背景

其它spring中的東西,基本都是下位替代,沒其它姿勢的時候可以勉強用一下

6.2使用

private void otherExample(){
    // 其下有各種轉義,用處有限
    System.out.println(StringEscapeUtils.class);
    // 資源載入工具類,但是不如springBoot註解好用
    System.out.println(ResourceUtils.class);
    // 讀取properties,馬馬虎虎的東西,java自帶的也不差
    System.out.println(LocalizedResourceHelper.class);
    // apache的IO包可太好用了,以及很多其它和apache重複的就不介紹了
    System.out.println(FileCopyUtils.class);
}

相關文章