AspectJ
AspectJ教程詳情訪問
https://www.yiibai.com/spring_aop/
bean配置
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<aop:aspectj-autoproxy/>
<bean id="arithmeticcalculator"class="com.imnu.dproxy.ArithmeticCalculatorImpl"></bean>
<bean id="loggingasceptj"class="com.imnu.dproxy.LoggingAsceptJ"></bean>
</beans>
切面類型
@Aspect
@Component
public class LoggingAsceptJ {
@Pointcut("execution(*com.imnu.dproxy.ArithmeticCalculatorImpl.*(..))")
privatevoid selectAll() {
}
@Before("selectAll()")
publicvoid beforeMethod(JoinPoint jpJoinPoint) {
Stringname = jpJoinPoint.getSignature().getName();
List<Object>arg = Arrays.asList(jpJoinPoint.getArgs());
System.out.println("這是前置通知Name" + name + "arg:" + arg);
}
@After("selectAll()")
publicvoid afterMethod(JoinPoint jpJoinPoint) {
Stringname = jpJoinPoint.getSignature().getName();
List<Object>arg = Arrays.asList(jpJoinPoint.getArgs());
System.out.println("這是後置通知Name" + name + "arg:" + arg);
}
@AfterReturning(pointcut= "selectAll()", returning = "retVal")
publicvoid afterReturning(JoinPoint jpJoinPoint, Object retVal) {
Stringname = jpJoinPoint.getSignature().getName();
List<Object>arg = Arrays.asList(jpJoinPoint.getArgs());
System.out.println("這是返回通知" + name + "arg:" + arg + "retVal:" +retVal);
}
@AfterThrowing(pointcut= "selectAll()", throwing = "error")
publicvoid afterThrowingAdvice(JoinPoint jp, Throwable error) {
System.out.println("[afterThrowingAdvice]Method Signature: " + jp.getSignature());
System.out.println("[afterThrowingAdvice]Exception: " + error);
}
/**
* 環繞通知需要攜帶ProceedingJoinPoint類型的參數
* 環繞通知類似於動態代理的全過程proceedingJoinPoint類型參數可以決定是否執行目標方法
* 且環繞通知必須有返回值,返回值爲目標方法的返回值
* @param proceedingJoinPoint
*/
@Around(value= "selectAll()")
publicObject anroudMethod(ProceedingJoinPoint proceedingJoinPoint) {
Objectresult = null;
StringmethodName = proceedingJoinPoint.getSignature().getName();
try{
System.out.println("前置通知");
result= proceedingJoinPoint.proceed();
System.out.println("後置通知");
}catch (Throwable e) {
System.out.println("異常通知");
e.printStackTrace();
}
returnresult;
}
}
Pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
測試代碼
ApplicationContext context = new ClassPathXmlApplicationContext("com/imnu/dproxy/bean.xml");
ArithmeticCalculator ari =(ArithmeticCalculator) context.getBean("arithmeticcalculator");
int resout = ari.add(1, 2);
System.out.println(resout);