序言
去年在項目當中引入了Lombok插件,着實解放了雙手,代替了一些重複的簡單工作(Getter,Setter,toString等方法的編寫)。
但是,在使用的過程當中,也發現了一些坑,開始的時候並沒有察覺到是Lombok的問題,後來跟蹤了對應的其他組件的源碼,才發現是Lombok的問題!
Setter-Getter方法的坑
問題發現
我們在項目當中主要使用Lombok的Setter-Getter方法的註解,也就是組合註解@Data,但是在一次使用Mybatis插入數據的過程當中,出現了一個問題,問題描述如下:
我們有個實體類:
@Data
public class NMetaVerify{
private NMetaType nMetaType;
private Long id;
....其他屬性
}
當我們使用Mybatis插入數據的時候,發現,其他屬性都能正常的插入,但是就是nMetaType屬性在數據庫一直是null.
解決
當我debug項目代碼到調用Mybatis的插入SQL對應的方法的時候,我看到NMetaVerify對象的nMetaType屬性還是有數據的,但是執行插入之後,數據庫的nMetaType字段就是一直是null.
原先我以爲是我的枚舉類型寫法不正確,看了下別的同樣具有枚舉類型的字段,也是正常能插入到數據庫當中的,這更讓我感覺到疑惑了.
於是,我就跟蹤Mybatis的源碼,發現Mybatis在獲取這個nMetaType屬性的時候使用了反射,使用的是getxxxx方法來獲取的,但是我發現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方法也是大寫
}
}
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;
}
}
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;
// 對應的get-set方法是getX();setX(int x);
//第二句話:name.length() > 1 && !Character.isUpperCase(name.charAt(1)))
// 屬性名字長度大於1,並且第二個(代碼中的charAt(1),這個1是數組下標)字母是小寫的
// 如果第二個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);//讓屬性名第一個字母小寫,然後加上後面的內容
}
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");
}
}
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)));
}
輸出結果如下:
方法名字是:isName 屬性名字:name
方法名字是:getName 屬性名字:name
方法名字是:getnMetaType 屬性名字:nMetaType //這個以及下面的屬性第二個字母都是大寫,所以直接返回name
方法名字是:getNMetaType 屬性名字:NMetaType
解決方案
修改屬性名字,讓第二個字母小寫,或者說是規定所有的屬性的前兩個字母必須小寫
如果數據庫已經設計好,並且前後端接口對接好了,不想修改,那就專門爲這種特殊的屬性使用idea生成get-set方法複製代碼
@Accessor(chain = true)註解的問題
問題發現
在使用easyexcel(github.com/alibaba/eas…) 導出的時候,發現以前的實體類導出都很正常,但是現在新加的實體類不正常了,比對了發現,新加的實體類增加了@Accessor(chain = true)註解,我們的目的主要是方便我們鏈式調用set方法:
new UserDto()
.setUserName("")
.setAge(10)
........
.setBirthday(new Date());
原因
easyexcel底層使用的是cglib來做反射工具包的:
com.alibaba.excel.read.listener.ModelBuildEventListener
類的第130行BeanMap.create(resultModel).putAll(map);
最底層的是cglib的BeanMap的這個方法調用
abstract public Object put(Object bean, Object key, Object value);
但是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);
}
}
解決方案
去掉Accessor註解
要麼就等待easyexcel的作者替換掉底層的cglib或者是其他,反正是支持獲取返回值不是void的setxxx方法就行復制代碼
個人博客網站:https://programmerblog.xyz
本文分享自微信公衆號 - IT技術分享社區(gh_a27c0758eb03)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。