您的位置:首页 > 编程语言 > ASP

Spring学习笔记十四---AspectJ返回通知,异常通知,环绕通知

2016-03-10 00:00 603 查看
package aopa;

import org.springframework.stereotype.Component;

@Component
public class CalImpl implements ICal {
@Override
public int add(int i, int j) {
return i + j;
}

@Override
public int sub(int i, int j) {
return i - j;
}

@Override
public int mul(int i, int j) {
return i * j;
}

@Override
public int div(int i, int j) {
return i/j;
}
}

package aopa;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Objects;

@Aspect
@Component
public class Logging {

/*
//前置通知
@Before("execution(public int aopa.CalImpl.*(..))")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("前置通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args));
}
//后置通知,无论该方法是否出现异常
@After("execution(public int aopa.CalImpl.*(..))")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("后置通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args));
}

//返回通知,可以访问方法返回值
@AfterReturning(value = "execution(public  int aopa.CalImpl.*(..))", returning = "result")
public void afterReturn(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("后置返回通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args) + " result : " + result);
}

//出现异常时候执行的代码
&nbs
3ff0
p;   @AfterThrowing(value = "execution(public  int aopa.CalImpl.*(..))", throwing = "ex")
public void afterThrow(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("异常通知 " +  "MethodName : " + methodName + " Exception : " + ex);
}
*/
//环绕通知ProceedingJoinPoint参数,是否执行目标方法,必须有目标方法返回值,相当于动态代理
@Around("execution(public  int aopa.CalImpl.*(..))")
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
Object[] args = proceedingJoinPoint.getArgs();
System.out.println("AroundMethod ....");
//执行目标方法
try {
//前置通知
System.out.println("前置通知 " +  "MethodName : " + methodName + " Args : " + Arrays.asList(args));
result = proceedingJoinPoint.proceed();
System.out.println("后置返回通知 " + result);
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("异常通知 " +  "MethodName : " + methodName + " Exception : " + throwable);
throw new RuntimeException();
}
System.out.println("后置通知 ");
return  result;
}
}

package aopa;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("aopa//application.xml");
ICal iCal = (ICal) ctx.getBean("calImpl");
System.out.println(iCal.getClass().getName());

int result = iCal.add(1, 1);
System.out.println("result : " + result);

result = iCal.div(10, 10);
System.out.println("result : " + result);
}
}

<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
<context:component-scan base-package="aopa"></context:component-scan>
<aop:aspectj-autoproxy/>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: