在使用cglib做动态代理时,报错
Caused by: net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InaccessibleObjectException
-->Unable to make protected final java.lang.Class
java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible:
module java.base does not "opens java.lang" to unnamed module @4bd4bcd4
主要代码如下:
package com.caozz.demo2.proxy.dynamic.cglib;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class MeipoCglib implements MethodInterceptor
{
public Object getInstance(Class clazz) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(clazz);
enhancer.setCallback(this);
return enhancer.create();
}
public void before(){
System.out.println("matchmaker---before()");
}
public void after(){
System.out.println("matchmaker---after()");
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
before();
Object result = methodProxy.invokeSuper(o, objects);
after();
return result;
}
}
解决方案:
vm参数新增(点击 RUN-Edit Configurations,将上述内容添加到 VM options 文本框中,如果没有,点击右边得Modify options,选择Add VM options)
--add-opens java.base/java.lang=ALL-UNNAMED
--add-exports java.base/jdk.internal.module=ALL-UNNAMED
网友评论