还是一样的需求
在执行计算的时候,给控制台答应的格式为
begain method with [ args1,args2 ]
end method with result
这次使用Spring AOP
定义接口:
package com.dxf.ArithmethicCalculatorSpringAOP;
public interface ArithmethicCalculator {
int add(int a, int b);
int sub(int a, int b);
int sul(int a, int b);
int div(int a, int b);
}
定义实现类:
package com.dxf.ArithmethicCalculatorSpringAOP;
import org.springframework.stereotype.Component;
@Component
public class ArithmethicCalculatorimp implements ArithmethicCalculator {
public int add(int a, int b) {
int result = a + b;
return result;
}
public int sub(int a, int b) {
int result = a - b;
return result;
}
public int sul(int a, int b) {
int result = a * b;
return result;
}
public int div(int a, int b) {
int result = a / b;
return result;
}
}
package com.dxf.ArithmethicCalculatorSpringAOP;
import java.util.Arrays;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* execution(int
* com.dxf.ArithmethicCalculatorSpringAOP.ArithmethicCalculatorimp.*(int, int))
* 格式为
*
* 返回类型 + 切点位置 +方法参数类型列表
*
* 在整个过程中,每次写一样的execution语句,而且还一样,就违背的代码的复用性了,那么就有了复用切点表达式
*
*
* 如果一个方法既要实现做日志,又要做验证,日志和验证谁在前,谁在后,可以使用Order,order 的值越小,执行越在前
*
*/
@Order(20)
@Aspect
@Component
public class ArithmethicCalculatorAspect {
// 编写一个切点表达式的方法
@Pointcut("execution(int com.dxf.ArithmethicCalculatorSpringAOP.ArithmethicCalculatorimp.*(int, int))")
public void PointCutexecution() {
}
/*
* 前置通知
*
* @Before("execution(int
* com.dxf.ArithmethicCalculatorSpringAOP.ArithmethicCalculatorimp.*(int,
* int))")
*/
@Before("PointCutexecution()")
public void methodBefor(JoinPoint joinPoint) {
System.out
.println("begain " + joinPoint.getSignature().getName() + " with" + Arrays.asList(joinPoint.getArgs()));
}
// 后置通知,即使有异常,此方法也会执行,所有在这里不能访问返回值
// @After("execution(int
// com.dxf.ArithmethicCalculatorSpringAOP.ArithmethicCalculatorimp.*(int,
// int))")
@After("PointCutexecution()")
public void methodAfter(JoinPoint joinPoint) {
System.out.println("end " + joinPoint.getSignature().getName() + " with" + Arrays.asList(joinPoint.getArgs()));
}
// 异常通知
// @AfterThrowing(value = "execution(int
// com.dxf.ArithmethicCalculatorSpringAOP.ArithmethicCalculatorimp.*(int,int))",
// throwing = "e")
@AfterThrowing(value = "PointCutexecution()", throwing = "e")
public void methodThrow(JoinPoint joinPoint, Exception e) {
System.out.println(joinPoint.getSignature().getName() + "发生异常: " + e.getMessage());
}
// 返回通知
// @AfterReturning(value = "execution(int
// com.dxf.ArithmethicCalculatorSpringAOP.ArithmethicCalculatorimp.*(int,
// int))", returning = "result") // 在此定义返回值得变量名
@AfterReturning(value = "PointCutexecution()", returning = "result")
public void methodAfterReturn(JoinPoint joinPoint, Object result) {
// System.out.println(value);
System.out.println("methodAfterReturn.....result " + result);
}
// 环绕通知,此通知就类似于一个代理类,上面的四种通知都在本通知中可以实现
// @Around(value = "execution(int
// com.dxf.ArithmethicCalculatorSpringAOP.ArithmethicCalculatorimp.*(int,
// int))")
@Around("PointCutexecution()")
public Object methodAroud(ProceedingJoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();// 获取参数列表
String method = joinPoint.getSignature().getName();// 获取方法名
Object result = null;
try {
// 前置通知
System.out.println("methodAroud before");
result = joinPoint.proceed();// 执行函数
System.out.println("methodAroud after");
// 后置通知
} catch (Throwable e) {
// 异常通知
System.out.println("methodAroud Throwable" + e.getMessage());
e.printStackTrace();
}
// 返回通知
System.out.println("methodAroud afterReturn " + result);
return 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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 声明使用aspect -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 启动自动扫描包 -->
<context:component-scan base-package="com.dxf.ArithmethicCalculatorSpringAOP"></context:component-scan>
</beans>
测试类:
package com.dxf.ArithmethicCalculatorSpringAOP;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"application-aop-annotation.xml");
ArithmethicCalculator arithmethicCalculator = applicationContext.getBean(ArithmethicCalculator.class);
System.out.println(arithmethicCalculator.div(1, 1));
}
}