目錄結構:java
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\StudentService.java
spring
package cn.edu.bjut.service; /** * Created by N3verL4nd on 2017/3/24. */ public interface StudentService { public void addStudent(String name); }D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\impl\StudentServiceImpl.java
package cn.edu.bjut.service.impl; import cn.edu.bjut.service.StudentService; /** * Created by N3verL4nd on 2017/3/24. */ public class StudentServiceImpl implements StudentService { @Override public void addStudent(String name) { System.out.println("[添加學生]:" + name); //觸發異常通知 // System.out.println( 1 / 0); } }D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\advice\StudentServiceAspect.java
package cn.edu.bjut.advice; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import java.util.Arrays; /** * Created by N3verL4nd on 2017/3/24. */ public class StudentServiceAspect { //前置通知 public void doBefore(JoinPoint jp) { System.out.println("--------------前置通知-------------"); System.out.println("類名:" + jp.getTarget().getClass().getName()); System.out.println("方法名:" + jp.getSignature().getName()); System.out.println("開始添加學生:" + Arrays.toString(jp.getArgs())); System.out.println("-----------------------------------"); } //後置通知 public void doAfter(JoinPoint jp) { System.out.println("--------------後置通知-------------"); System.out.println("學生添加完成!"); System.out.println("-----------------------------------"); } //環繞通知 public Object doAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("--------------環繞通知-------------"); System.out.println("添加學生前:"); Object retVal = null; retVal= pjp.proceed(); System.out.println("返回值:" + retVal); System.out.println("添加學生後!"); System.out.println("-----------------------------------"); return retVal; } //返回通知 public void doAfterReturning(JoinPoint jp) { System.out.println("--------------返回通知-------------"); System.out.println("-----------------------------------"); } //異常通知 public void doAfterThrowing(JoinPoint jp, Throwable ex) { System.out.println("--------------異常通知-------------"); System.out.println("異常信息:" + ex.getMessage()); System.out.println("-----------------------------------"); } }D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\test\T.java
package cn.edu.bjut.test; import cn.edu.bjut.service.StudentService; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by N3verL4nd on 2017/3/24. */ public class T { private ApplicationContext context = null; @Before public void setUp() { context = new ClassPathXmlApplicationContext("beans.xml"); } @Test public void test() { StudentService studentService = (StudentService) context.getBean("studentService"); studentService.addStudent("lgh"); } }D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\beans.xml
<?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"> <bean id="studentServiceAspect" class="cn.edu.bjut.advice.StudentServiceAspect"/> <bean id="studentService" class="cn.edu.bjut.service.impl.StudentServiceImpl"/> <aop:config> <aop:aspect ref="studentServiceAspect"> <aop:pointcut id="businessService" expression="execution(public void cn.edu.bjut.service.impl.StudentServiceImpl.addStudent(String))"/> <aop:before method="doBefore" pointcut-ref="businessService"/> <aop:after method="doAfter" pointcut-ref="businessService"/> <aop:around method="doAround" pointcut-ref="businessService"/> <aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/> <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/> </aop:aspect> </aop:config> </beans>輸出:
--------------前置通知-------------
類名:cn.edu.bjut.service.impl.StudentServiceImpl
方法名:addStudent
開始添加學生:[lgh]
-----------------------------------
--------------環繞通知-------------
添加學生前:
[添加學生]:lgh
--------------返回通知-------------
-----------------------------------
返回值:null
添加學生後!
-----------------------------------
--------------後置通知-------------
學生添加完成!
-----------------------------------
express
介紹一下org/aspectj/lang/JoinPoint.javaide
AspectJ使用org.aspectj.lang.JoinPoint接口表示目標類鏈接點對象,若是是環繞加強時,使用org.aspectj.lang.ProceedingJoinPoint表示鏈接點對象,該類是JoinPoint的子接口。spa
任何一個加強方法均可以經過將第一個入參聲明爲JoinPoint訪問到鏈接點上下文的信息。咱們先來了解一下這兩個接口的主要方法:
代理
1)JoinPoint
java.lang.Object[] getArgs():獲取鏈接點方法運行時的入參列表;
Signature getSignature() :獲取鏈接點的方法簽名對象;
java.lang.Object getTarget() :獲取鏈接點所在的目標對象;
java.lang.Object getThis() :獲取代理對象自己;
2)ProceedingJoinPoint
ProceedingJoinPoint繼承JoinPoint子接口,它新增了兩個用於執行鏈接點方法的方法:
java.lang.Object proceed() throws java.lang.Throwable:經過反射執行目標對象的鏈接點處的方法;
java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:經過反射執行目標對象鏈接點處的方法,不過使用新的入參替換原來的入參。
xml