public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
public static final Logger LOGGER = LoggerFactory.getLogger(ArithmeticCalculator.class);
@Override
public int add(int i, int j) {
LOGGER.info("The method add begins with [{}, {}]", i, j);
int result = i + j;
LOGGER.info("The method add ends with {}", result);
return result;
}
@Override
public int sub(int i, int j) {
LOGGER.info("The method sub begins with [{}, {}]", i, j);
int result = i - j;
LOGGER.info("The method sub ends with {}", result);
return result;
}
@Override
public int mul(int i, int j) {
LOGGER.info("The method mul begins with [{}, {}]", i, j);
int result = i * j;
LOGGER.info("The method mul ends with {}", result);
return result;
}
@Override
public int div(int i, int j) {
LOGGER.info("The method div begins with [{}, {}]", i, j);
int result = i / j;
LOGGER.info("The method div ends with {}", result);
return result;
}
}
public class Main {
public static void main(String[] args) {
ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorImpl();
int result = arithmeticCalculator.add(1, 2);
System.out.println(result);
result = arithmeticCalculator.sub(4, 2);
System.out.println(result);
}
}
输出结果如下:
23:01:49.551 [main] INFO com.example.springdemo.aop.helloworld.ArithmeticCalculator - The method add begins with [1, 2]
23:01:49.553 [main] INFO com.example.springdemo.aop.helloworld.ArithmeticCalculator - The method add ends with 3
3
23:01:49.553 [main] INFO com.example.springdemo.aop.helloworld.ArithmeticCalculator - The method sub begins with [4, 2]
23:01:49.553 [main] INFO com.example.springdemo.aop.helloworld.ArithmeticCalculator - The method sub ends with 2
2
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
public static final Logger LOGGER = LoggerFactory.getLogger(ArithmeticCalculator.class);
@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;
}
}
public class Main {
public static void main(String[] args) {
ArithmeticCalculator target = new ArithmeticCalculatorImpl();
ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();
int result = proxy.add(1, 2);
System.out.println(result);
proxy.sub(4, 2);
System.out.println(result);
}
}
输出结果如下所示:
The method add begins with [1, 2]
The method add ends with 3
3
The method sub begins with [4, 2]
The method sub ends with 2
3
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@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;
}
}
/**
* 把该类声明为切面
* 1. 需要把该类放入到 IoC 容器中;
* 2. 然后添加一个切面注解
*
* @date 2021/07/28
*/
@Aspect
@Component
public class LoggingAspect {
// 前置通知:在目标方法开始之前执行
@Before("execution(public int com.example.springdemo.aop.impl.ArithmeticCalculator.add(int, int))")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List
<?xml version="1.0" encoding="UTF-8"?>
public class Main {
public static void main(String[] args) {
// 创建 Spring 的 IoC 容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextAop.xml");
// 从容器中获取 bean 的实例
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) context.getBean("arithmeticCalculator");
// 使用 bean
int result = arithmeticCalculator.add(1, 2);
System.out.println("add result: " + result);
result = arithmeticCalculator.div(9 , 3);
System.out.println("div result: " + result);
}
}
输出结果如下:
The method addbegins with [1, 2]
add result: 3
div result: 3
@Aspect
@Component
public class LoggingAspect {
// 前置通知:在目标方法开始之前执行
@Before("execution(public int com.example.springdemo.aop.impl.ArithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
List