学习Spring AOP时报错:java.lang.instrument ASSERTION FAILED

····在学习《深入浅出spring-boot》第四章第一节时,敲完书中的代码没有得到想要的约定流程结果,并且报了个错。
完整错误:

java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844

测试代码:

public static void main(String[] args) {
        HelloService helloService = new HelloServiceImpl();
        HelloService proxy =(HelloService) ProxyBean.getProxyBean(helloService,new MyInterceptor());
        proxy.sayHello("麦克*格雷迪");
    }

服务接口代码:

package com.kane.springboot002.service;

/**
 * 〈一句话功能简述〉
* 〈jiekou〉 * * @author dell * @create 2019/5/27 * @since 1.0.0 */ public interface HelloService { public void sayHello(String name); }

服务实现类代码:

package com.kane.springboot002.service;

/**
 * 〈一句话功能简述〉
* 〈shixian〉 * * @author Kane * Date: 2019/5/27 16:09 * @since 1.0.0 */ public class HelloServiceImpl implements HelloService{ @Override public void sayHello(String name) { if(name == null || name.trim().equals("")){ throw new RuntimeException("sayHello(方法传参为空或空字符串)"); } System.out.println("Hello!"+name+"."); } }

拦截器接口代码:

package com.kane.springboot002.interceptor;

import java.lang.reflect.InvocationTargetException;

/**
 * 〈一句话功能简述〉
* 〈拦截器〉 * * @author dell * @create 2019/5/27 * @since 1.0.0 */ public interface Interceptor { //事前方法 boolean before() ; //事后方法 void after (); Object around(Invocation invocation) throws InvocationTargetException,IllegalAccessException; //是否返回方法 。 事件没有发生异常执行 void afterReturning() ; //事后异常方法 , 当事件发生异常后执行 void afterThrowing (); //是否使用 around 方法取代原有方法 boolean useAround() ; }

拦截器实现类代码:

package com.kane.springboot002.interceptor;

import java.lang.reflect.InvocationTargetException;

/**
 * 〈一句话功能简述〉
* 〈自定义拦截器〉 * * @author Kane * Date: 2019/5/27 16:26 * @since 1.0.0 */ public class MyInterceptor implements Interceptor{ @Override public boolean before() { System.out.println("MyInterceptor before()..."); return true; } @Override public void after() { System.out.println("MyInterceptor after()..."); } @Override public Object around(Invocation invocation) throws InvocationTargetException, IllegalAccessException { System.out.println("MyInterceptor around()...begin"); Object object = invocation.proceed(); System.out.println("MyInterceptor around()...end"); return object; } @Override public void afterReturning() { System.out.println("MyInterceptor afterReturning()..."); } @Override public void afterThrowing() { System.out.println("MyInterceptor afterThrowing()..."); } @Override public boolean useAround() { System.out.println("MyInterceptor useAround()..."); return true; } }

Invocation类代码:

package com.kane.springboot002.interceptor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 〈一句话功能简述〉
* 〈ss〉 * * @author Kane * Date: 2019/5/27 16:21 * @since 1.0.0 */ public class Invocation { private Object[] params; private Method method; private Object target; public Invocation(Object[] params, Method method, Object target) { this.params = params; this.method = method; this.target = target; } //反射方法 public Object proceed() throws InvocationTargetException,IllegalAccessException{ return method.invoke(target,params); } /**get and set**/ public Object[] getParams() { return params; } public void setParams(Object[] params) { this.params = params; } public Method getMethod() { return method; } public void setMethod(Method method) { this.method = method; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } }

代理类代码:

package com.kane.springboot002.proxy;

import com.kane.springboot002.interceptor.Interceptor;
import com.kane.springboot002.interceptor.Invocation;
import com.kane.springboot002.interceptor.MyInterceptor;
import com.kane.springboot002.service.HelloService;
import com.kane.springboot002.service.HelloServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 〈一句话功能简述〉
* 〈ss〉 * * @author Kane * Date: 2019/5/28 8:55 * @since 1.0.0 */ public class ProxyBean implements InvocationHandler { private Object target = null; private Interceptor interceptor = null; /** * 绑定代理对象 * @param target * @param interceptor * @return */ public static Object getProxyBean(Object target,Interceptor interceptor){ ProxyBean proxyBean = new ProxyBean(); proxyBean.target = target; proxyBean.interceptor = interceptor; //生成代理对象 Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),proxyBean); return proxy; } /** *处理代理对象方法逻辑 * @param proxy * @param method * @param args * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) { boolean exceptionFlag = false; Invocation invocation = new Invocation(args,method,proxy); Object retObj = null; this.interceptor.before(); try { if(this.interceptor.useAround()){ retObj = this.interceptor.around(invocation); }else { retObj = method.invoke(target,args); } }catch (Exception e){ exceptionFlag = true; } this.interceptor.after(); if (exceptionFlag){ this.interceptor.afterThrowing(); }else { this.interceptor.afterReturning(); return retObj; } return null; } public static void main(String[] args) { HelloService helloService = new HelloServiceImpl(); HelloService proxy =(HelloService) ProxyBean.getProxyBean(helloService,new MyInterceptor()); proxy.sayHello("麦克*格雷迪"); } }

特此记录,以期后续!

你可能感兴趣的:(java,spring,aop)