//接口
package com.atguigu.inter;
public interface Calculator {
public int add(int a, int b);
public int sub(int a, int b);
public int mul(int a, int b);
public int div(int a, int b);
}
//实现类
package com.atguigu.impl;
import com.atguigu.inter.Calculator;
import org.springframework.stereotype.Service;
@Service
public class MyMathCalculator implements Calculator {
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public int sub(int a, int b) {
return a-b;
}
@Override
public int mul(int a, int b) {
return a*b;
}
@Override
public int div(int a, int b) {
return a/b;
}
}
//第一个切面类
package com.atguigu.utils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
public class ValidateAspect {
public static void logStart(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();//获取目标方法运行时使用的参数
Signature signature = joinPoint.getSignature();//获取到方法签名
String name = signature.getName();
System.out.println("[validateAspect]["+ name +"]" + "方法开始执行了,用的参数列表是:["+ Arrays.asList(args) +"]");
}
public static void logReturn(JoinPoint joinPoint, Object result){
Signature signature = joinPoint.getSignature();//获取到方法签名
String name = signature.getName();
System.out.println("[validateAspect]["+ name +"]" + "方法正常执行完成了,计算结果是:"+result);
}
public static void logException(JoinPoint joinPoint, Exception e){
System.out.println("[validateAspect]["+ joinPoint.getSignature().getName() +"]"+ "出现了异常,异常原因是:"+"["+e+"]");
}
public static void logEnd(JoinPoint joinPoint){
System.out.println("[validateAspect]["+ joinPoint.getSignature().getName() +"]" + "方法执行结束了。");
}
}