<context:component-scan base-package="per.tan"/>
<aop:aspectj-autoproxy/>
public interface Calc {
Integer add(Integer num1, Integer num2);
Integer min(Integer num1, Integer num2);
Integer mul(Integer num1, Integer num2);
Integer div(Integer num1, Integer num2);
}
@Component
public class CalcImpl implements Calc {
@Override
public Integer add(Integer num1, Integer num2) {
Integer result = num1 + num2;
System.out.println("执行业务,完成了一次加法!");
return result;
}
@Override
public Integer min(Integer num1, Integer num2) {
Integer result = num1 - num2;
System.out.println("执行业务,完成了一次减法!");
return result;
}
@Override
public Integer mul(Integer num1, Integer num2) {
Integer result = num1 * num2;
System.out.println("执行业务,完成了一次乘法!");
return result;
}
@Override
public Integer div(Integer num1, Integer num2) {
Integer result = num1 / num2;
System.out.println("执行业务,完成了一次除法!");
return result;
}
}
@Component
@Aspect
public class LogAspect {
//指定位置,方法通配符 * ,参数通配符 .. 。
@Before(value = "execution(public Integer per.tan.aop.CalcImpl.*(..))")
public void before(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println(name + "之前记录日志[Before]" + ",参数为:" + args);
}
@After(value = "execution(public Integer per.tan.aop.CalcImpl.*(..))")
public void after(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
System.out.println(name + "之后记录日志[After]");
}
@AfterReturning(value = "execution(public Integer per.tan.aop.CalcImpl.*(..))", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String name = joinPoint.getSignature().getName();
System.out.println(name + "之后记录日志[AfterReturning],并拿到返回值:" + result);
}
@AfterThrowing(value = "execution(public Integer per.tan.aop.CalcImpl.*(..))", throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Exception e) {
String name = joinPoint.getSignature().getName();
System.out.println(name + "之后记录日志[afterThrowing],异常信息为:" + e);
}
}
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Calc calc = context.getBean(Calc.class);
calc.add(10, 2);
System.out.println();
calc.min(10, 2);
System.out.println();
calc.min(10, 2);
System.out.println();
calc.div(10, 2);
System.out.println();
calc.div(10, 0);
}
}
D:\Java_JDK\JDK8\bin\java.exe ...
五月 16, 2021 4:19:15 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7daf6ecc: startup date [Sun May 16 16:19:15 CST 2021]; root of context hierarchy
五月 16, 2021 4:19:15 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
add之前记录日志[Before],参数为:[10, 2]
执行业务,完成了一次加法!
add之后记录日志[After]
add之后记录日志[AfterReturning],并拿到返回值:12
min之前记录日志[Before],参数为:[10, 2]
执行业务,完成了一次减法!
min之后记录日志[After]
min之后记录日志[AfterReturning],并拿到返回值:8
min之前记录日志[Before],参数为:[10, 2]
执行业务,完成了一次减法!
min之后记录日志[After]
min之后记录日志[AfterReturning],并拿到返回值:8
div之前记录日志[Before],参数为:[10, 2]
执行业务,完成了一次除法!
div之后记录日志[After]
div之后记录日志[AfterReturning],并拿到返回值:5
div之前记录日志[Before],参数为:[10, 0]
div之后记录日志[After]
div之后记录日志[afterThrowing],异常信息为:java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at per.tan.aop.CalcImpl.div(CalcImpl.java:36)
原文:https://www.cnblogs.com/200ok/p/14774054.html