問題
這個noValue一定存在,但是報錯。
場景就是存在並發的情況下,尤其是在服務剛剛啟動的時候,就會發生這個異常。
但是很不幸,mybatis 3.4.1之前,用的 OGNL都是由這個問題。
分析
3.4.1 之前的版本的 OgnlRuntime,這里,第三個參數傳0,則永遠都是null。
public static final Object getMethodValue(OgnlContext context, Object target, String propertyName, boolean checkAccessAndExistence) throws OgnlException, IllegalAccessException, NoSuchMethodException, IntrospectionException {
Object result = null;
Method m = getGetMethod(context, target == null ? null : target.getClass(), propertyName);
if (m == null) {
m = getReadMethod(target == null ? null : target.getClass(), propertyName, 0);
}
3.4.1 以及以后的版本:
public static final Object getMethodValue(OgnlContext context, Object target, String propertyName, boolean checkAccessAndExistence) throws OgnlException, IllegalAccessException, NoSuchMethodException, IntrospectionException {
Object result = null;
Method m = getGetMethod(context, target == null ? null : target.getClass(), propertyName);
if (m == null) {
m = getReadMethod(target == null ? null : target.getClass(), propertyName, (Class[])null);
}
顯然 getReadMethod 這個地方的實現已經完全發生改變。
而
getGetMethod 存在 並發問題,線程不安全。