Lombok的坑有哪些

Lombok的坑有哪些?這個問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

成都創(chuàng)新互聯(lián)公司始終堅持【策劃先行,效果至上】的經(jīng)營理念,通過多達10年累計超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)營銷解決方案,現(xiàn)已廣泛運用于各行各業(yè)的客戶,其中包括:成都酒樓設(shè)計等企業(yè),備受客戶贊揚。

序言

去年在項目當(dāng)中引入了Lombok插件,著實解放了雙手,代替了一些重復(fù)的簡單工作(Getter,Setter,toString等方法的編寫),但是,在使用的過程當(dāng)中,也發(fā)現(xiàn)了一些坑,開始的時候并沒有察覺到是Lombok的問題,后來跟蹤了對應(yīng)的其他組件的源碼,才發(fā)現(xiàn)是Lombok的問題!

Setter-Getter方法的坑

問題發(fā)現(xiàn)

我們在項目當(dāng)中主要使用Lombok的Setter-Getter方法的注解,也就是組合注解@Data,但是在一次使用Mybatis插入數(shù)據(jù)的過程當(dāng)中,出現(xiàn)了一個問題,問題描述如下:

我們有個實體類:
@Data
public class NMetaVerify{
    private NMetaType nMetaType;
    private Long id;
    ....其他屬性
}復(fù)制代碼

當(dāng)我們使用Mybatis插入數(shù)據(jù)的時候,發(fā)現(xiàn),其他屬性都能正常的插入,但是就是nMetaType屬性在數(shù)據(jù)庫一直是null.

解決

當(dāng)我debug項目代碼到調(diào)用Mybatis的插入SQL對應(yīng)的方法的時候,我看到NMetaVerify對象的nMetaType屬性還是有數(shù)據(jù)的,但是執(zhí)行插入之后,數(shù)據(jù)庫的nMetaType字段就是一直是null,原先我以為是我的枚舉類型寫法不正確,看了下別的同樣具有枚舉類型的字段,也是正常能插入到數(shù)據(jù)庫當(dāng)中的,這更讓我感覺到疑惑了.于是,我就跟蹤Mybatis的源碼,發(fā)現(xiàn)Mybatis在獲取這個nMetaType屬性的時候使用了反射,使用的是getxxxx方法來獲取的,但是我發(fā)現(xiàn)nMetaType的get方法好像有點和Mybatis需要的getxxxx方法長的好像不一樣.問題找到了!

原因

Lombok對于第一個字母小寫,第二個字母大寫的屬性生成的get-set方法和Mybatis以及idea或者說是Java官方認可的get-set方法生成的不一樣:

#Lombok生成的Get-Set方法
@Data
public class NMetaVerify {
    private Long id;
    private NMetaType nMetaType;
    private Date createTime;
    
    public void lombokFound(){
        NMetaVerify nMetaVerify = new NMetaVerify();
        nMetaVerify.setNMetaType(NMetaType.TWO); //注意:nMetaType的set方法為setNMetaType,第一個n字母大寫了,
        nMetaVerify.getNMetaType();                                  //getxxxx方法也是大寫
    }
}復(fù)制代碼
#idea,Mybatis,Java官方默認的行為為:
public class NMetaVerify {
    private Long id;
    private NMetaType nMetaType;
    private Date createTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public NMetaType getnMetaType() {//注意:nMetaType屬性的第一個字母小寫
        return nMetaType;
    }

    public void setnMetaType(NMetaType nMetaType) {//注意:nMetaType屬性的第一個字母小寫
        this.nMetaType = nMetaType;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}復(fù)制代碼
Mybatis(3.4.6版本)解析get-set方法獲取屬性名字的源碼:
package org.apache.ibatis.reflection.property;

import java.util.Locale;

import org.apache.ibatis.reflection.ReflectionException;

/**
 * @author Clinton Begin
 */
public final class PropertyNamer {

        private PropertyNamer() {
            // Prevent Instantiation of Static Class
        }

        public static String methodToProperty(String name) {
            if (name.startsWith("is")) {//is開頭的一般是bool類型,直接從第二個(索引)開始截取(簡單粗暴)
                    name = name.substring(2);
            } else if (name.startsWith("get") || name.startsWith("set")) {//set-get的就從第三個(索引)開始截取
                    name = name.substring(3);
            } else {
                    throw new ReflectionException("Error parsing property name '" + name + "'.  Didn't start with 'is', 'get' or 'set'.");
            }
            //下面這個判斷很重要,可以分成兩句話開始解釋,解釋如下
            //第一句話:name.length()==1
            //                      對于屬性只有一個字母的,例如private int x;
            //                      對應(yīng)的get-set方法是getX();setX(int x);
            //第二句話:name.length() > 1 && !Character.isUpperCase(name.charAt(1)))
            //                      屬性名字長度大于1,并且第二個(代碼中的charAt(1),這個1是數(shù)組下標(biāo))字母是小寫的
            //                      如果第二個char是大寫的,那就直接返回name
            if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
                    name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);//讓屬性名第一個字母小寫,然后加上后面的內(nèi)容
            }

            return name;
        }

        public static boolean isProperty(String name) {
                return name.startsWith("get") || name.startsWith("set") || name.startsWith("is");
        }

        public static boolean isGetter(String name) {
                return name.startsWith("get") || name.startsWith("is");
        }

        public static boolean isSetter(String name) {
                return name.startsWith("set");
        }

}復(fù)制代碼
Mybatis解析get-set方法為屬性名字測試
    @Test
    public void foundPropertyNamer() {
        String isName = "isName";
        String getName = "getName";
        String getnMetaType = "getnMetaType";
        String getNMetaType = "getNMetaType";

        Stream.of(isName,getName,getnMetaType,getNMetaType)
                .forEach(methodName->System.out.println("方法名字是:"+methodName+" 屬性名字:"+ PropertyNamer.methodToProperty(methodName)));
    }
    
    #輸出結(jié)果如下:
    方法名字是:isName 屬性名字:name 
    方法名字是:getName 屬性名字:name 
    方法名字是:getnMetaType 屬性名字:nMetaType //這個以及下面的屬性第二個字母都是大寫,所以直接返回name
    方法名字是:getNMetaType 屬性名字:NMetaType復(fù)制代碼

解決方案

1.修改屬性名字,讓第二個字母小寫,或者說是規(guī)定所有的屬性的前兩個字母必須小寫
2.如果數(shù)據(jù)庫已經(jīng)設(shè)計好,并且前后端接口對接好了,不想修改,那就專門為這種特殊的屬性使用idea生成get-set方法復(fù)制代碼

@Accessor(chain = true)注解的問題

問題發(fā)現(xiàn)

在使用easyexcel(github.com/alibaba/eas…) 導(dǎo)出的時候,發(fā)現(xiàn)以前的實體類導(dǎo)出都很正常,但是現(xiàn)在新加的實體類不正常了,比對了發(fā)現(xiàn),新加的實體類增加了@Accessor(chain = true)注解,我們的目的主要是方便我們鏈?zhǔn)秸{(diào)用set方法:

new UserDto()
.setUserName("")
.setAge(10)
........
.setBirthday(new Date());復(fù)制代碼

原因

easyexcel底層使用的是cglib來做反射工具包的:

com.alibaba.excel.read.listener.ModelBuildEventListener 類的第130行
BeanMap.create(resultModel).putAll(map);

最底層的是cglib的BeanMap的這個方法調(diào)用

abstract public Object put(Object bean, Object key, Object value);復(fù)制代碼

但是cglib使用的是Java的rt.jar里面的一個Introspector這個類的方法:

# Introspector.java 第520行
if (int.class.equals(argTypes[0]) && name.startsWith(GET_PREFIX)) {
   pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, method, null);
   //下面這行判斷,只獲取返回值是void類型的setxxxx方法
 } else if (void.class.equals(resultType) && name.startsWith(SET_PREFIX)) {
    // Simple setter
    pd = new PropertyDescriptor(this.beanClass, name.substring(3), null, method);
    if (throwsException(method, PropertyVetoException.class)) {
       pd.setConstrained(true);
    }
}復(fù)制代碼

解決方案

1.去掉Accessor注解
2.要么就等待easyexcel的作者替換掉底層的cglib或者是其他,反正是支持獲取返回值不是void的setxxx方法就行復(fù)制代碼

感謝各位的閱讀!看完上述內(nèi)容,你們對Lombok的坑有哪些大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:Lombok的坑有哪些
網(wǎng)頁地址:http://bm7419.com/article42/ipoeec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站ChatGPT虛擬主機、定制開發(fā)App開發(fā)、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管