1、首先建立一个接口,建立一个实现接口的实现类。
package com.test.spring.aop.impl;
import org.springframework.stereotype.Component;
interface ArithmeticCalaculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
package com.test.spring.aop.impl;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalaculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
package com.test.spring.aop.impl;
import java.util.Arrays;
import javax.management.RuntimeErrorException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
//此类是切面所以要加一个特殊注解
@Component
@Aspect
public class LoggingAspect {
/*
* 在com.test.spring.aop.ArithmeticCalactulato接口每个实现类的每一个方法 使之前执行一段代码
* ArithmeticCalaculator 前置通知
*/
@Before("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public void beforemethod(JoinPoint joint) {
String methodname = joint.getSignature().getName();
Object[] args = joint.getArgs();
System.out.println("the mehtod" + methodname + " begins with:"
+ Arrays.asList(args));
}
/*
* 后置通知 在目标方法执行之后进行执行
*/
@After("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public void afterMethod(JoinPoint joint) {
// 获取目标方法名称
String methodname = joint.getSignature().getName();
Object[] obj = joint.getArgs();
System.out.println("the method:" + methodname + " end with args"
+ Arrays.asList(obj));
}
/*
* 有些方法是在目标方法正常执行之后执行。返回通知 返回通知是可以访问到方法的返回值的。
*/
@AfterReturning(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", returning = "result")
public void afterReturn(JoinPoint point, Object result) {
String mehtodname = point.getSignature().getName();
System.out.println("the method------ bengin" + mehtodname
+ " end with:" + result);
}
/*
* 在目标方法出现异常时候执行的代码 可以访问到异常;且可以指定异常的类型。 异常通知。如果目标方法中存在异常。这时候。会执行这个方法
*/
@AfterThrowing(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", throwing = "ex")
public void afterThrowing(JoinPoint point, Exception ex) {
String mehtodname = point.getSignature().getName();
System.out.println("the method------ bengin" + mehtodname
+ " occur with:" + ex);
}
/*
* Around环绕通知 环绕通知执行的方法需要在参数里使用ProcedingJoinPoin类型的参数
* 其中环绕通知类似于动态代理的全过程。就是ProcedingJoinPoint类型的参数决定是否执行目标通知
* 且环绕通知必须有返回值。返回值即为目标方法的返回值。
*/
/*
* 只做了解即可
*/
@Around(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pro) {
// 执行目标方法
Object result = null;
String methodname = pro.getSignature().getName();
try {
// 前置通知
System.out.println("Th==-=-=-start-=-=-= method" + methodname
+ "begins with" + Arrays.asList(pro.getArgs()));
result = pro.proceed();
// 后置通知
System.out.println("Th==-=-=-end=-=-=-= method" + methodname
+ "end with" + result);
} catch (Throwable e) {
// 异常通知
System.out.println("The method occurs exception" + e);
throw new RuntimeErrorException((Error) e);
}
System.out.println(" the aroundMethod is" + methodname + "ends");
return result;
}
}
2、在实现类中我们为其采用注解加入ioc容器的配置方法。并且在该类的上部加入注解的形式为
@Component("arithmeticCalculator")
3.建立一个测试Aspect类。我们也为其加入到ioc容器中采用注解的方式进行注入。
同时为其加入特殊的切面注解类。
@Aspect
切面类完成之后我们可以在这个类里进行写方法在方法上边需要加上注解:常见的注解有五种形式
分别对应目标方法执行的流程的前后。
1.@Before 对目标方法的调用前的执行
@Before("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public void beforemethod(JoinPoint joint) {
String methodname = joint.getSignature().getName();
Object[] args = joint.getArgs();
System.out.println("the mehtod" + methodname + " begins with:"
+ Arrays.asList(args));
}
2.@After 对目标方法的调用后的执行
@After("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public void afterMethod(JoinPoint joint) {
// 获取目标方法名称
String methodname = joint.getSignature().getName();
Object[] obj = joint.getArgs();
System.out.println("the method:" + methodname + " end with args"
+ Arrays.asList(obj));
}
3.@AfterReturning 有些方法是在目标方法正常执行之后执行需要返回通知 返回通知是可以访问到方法的返回值的。
/*
* 有些方法是在目标方法正常执行之后执行。返回通知 返回通知是可以访问到方法的返回值的。
*/
@AfterReturning(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", returning = "result")
public void afterReturn(JoinPoint point, Object result) {
String mehtodname = point.getSignature().getName();
System.out.println("the method------ bengin" + mehtodname+ " end with:" + result);
}
4.@AfterThrowing 在目标方法出现异常时候执行的代码 可以访问到异常;且可以指定异常的类型。 异常通知。如果目标方法中存在异常。这时候。会执行这个方法
@AfterThrowing(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", throwing = "ex")
public void afterThrowing(JoinPoint point, Exception ex) {
String mehtodname = point.getSignature().getName();
System.out.println("the method------ bengin" + mehtodname
+ " occur with:" + ex);
}
5.@Around /*
* Around环绕通知 环绕通知执行的方法需要在参数里使用ProcedingJoinPoin类型的参数
* 其中环绕通知类似于动态代理的全过程。就是ProcedingJoinPoint类型的参数决定是否执行目标通知
* 且环绕通知必须有返回值。返回值即为目标方法的返回值。
*/
@Around(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pro) {
// 执行目标方法
Object result = null;
String methodname = pro.getSignature().getName();
try {
// 前置通知
System.out.println("Th==-=-=-start-=-=-= method" + methodname
+ "begins with" + Arrays.asList(pro.getArgs()));
result = pro.proceed();
// 后置通知
System.out.println("Th==-=-=-end=-=-=-= method" + methodname
+ "end with" + result);
} catch (Throwable e) {
// 异常通知
System.out.println("The method occurs exception" + e);
throw new RuntimeErrorException((Error) e);
}
System.out.println(" the aroundMethod is" + methodname + "ends");
return result;
}
}