package aspectj; public interface MyInterface { void objectMethod(String in); }
package aspectj; public class MyClass implements MyInterface { @Override public void objectMethod(String in) { System.out.println(in); throw new RuntimeException(); } }
package aspectj; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class MyAdvice { @Around(value = "execution(public * *(..))") public Object myAdvice(ProceedingJoinPoint joinPoint) throws Throwable { try{ System.out.println("joinPoint--1"); return joinPoint.proceed(new String[]{"joinPoint"}); }catch(RuntimeException e){ System.out.println("catch"); return null; } } @Around(value = "execution(public * *(..))") public Object myAdvice2(ProceedingJoinPoint joinPoint) throws Throwable { try{ System.out.println("joinPoint--2"); return joinPoint.proceed(); }catch(RuntimeException e){ System.out.println("catch2"); return null; } } }
package aspectj; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml"},true); MyInterface myInterface= (MyInterface) applicationContext.getBean("MyClass"); myInterface.objectMethod("Hello"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy /> <bean id="MyClass" class="aspectj.MyClass"/> <bean class="aspectj.MyAdvice"/> </beans>
xml的文件名如java文件所示,放在src目录下
上面的代码是基本款,运行正常,下面说说特殊情况
MyAdvice里面有两个around,前面的先执行,然后调用后面的,相当于前面的around后面的
通过给JoinPoint.proceed()传递参数,可以替换掉原来的参数,可是当参数的数量或类型不匹配的时候会报错,所以简单点直接调用无参方法即可
对于原方法是void但advice中却又return的情况,会被直接忽视,所以如代码所建,void方法也return了
更多的功能可以调用JoinPoint的方法来实现
另外如果MyClass实现了多个接口,则得到的类也实现了那些接口
如果MyClass继承了别的类,得到的代理类不会继承那个内,这也意味着得到的肯定不再是MyClass了