但行好事,莫问前程
7、基于XML的AOP(了解)
7.1、准备工作
1 添加依赖
<!-- spring-aspects会帮我们传递过来aspectjweaver -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.22</version>
</dependency>
2 目标资源
接口
public interface Calculator {
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
public class CalculatorPureImpl implements Calculator {
@Override
public int add(int i, int j) {
int result = i + j;
System.out.println("方法内部result=" + result);
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
System.out.println("方法内部result=" + result);
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
System.out.println("方法内部result=" + result);
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
System.out.println("方法内部result=" + result);
return result;
}
}
7.2、实现
1 LogAspect 切面类
@Component // @Component注解保证这个切面类能够放入IOC容器
public class LogAspect {
public void beforeMethod(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
String s = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:" + name + ",参数:" + s);
}
public void afterMethod(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
System.out.println("Logger-->后置通知,方法名:" + name);
}
public void afterReturningMethod(JoinPoint joinPoint, Object result) {
String name = joinPoint.getSignature().getName();
System.out.println("Logger-->返回通知,方法名:" + name + "结果:" + result);
}
public void afterThrowingMehtod(JoinPoint joinPoint, Throwable ex) {
String name = joinPoint.getSignature().getName();
System.out.println("Logger-->异常通知,方法名:" + name + "异常:" + ex);
}
public Object aroundMethod(ProceedingJoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
Object result = null;
try {
System.out.println("环绕通知-->目标对象方法执行之前");
//目标对象(连接点)方法的执行
result = joinPoint.proceed();
System.out.println("环绕通知-->目标对象方法返回值之后");
} catch (Throwable e) {
System.out.println("环绕通知-->目标对象方法出现异常时");
throw new RuntimeException(e);
} finally {
System.out.println("环绕通知-->目标对象方法执行完毕");
}
return result;
}
}
2 ValidateAspect 切面类
@Component
public class ValidateAspect {
public void beforeMethod(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
String s = Arrays.toString(joinPoint.getArgs());
System.out.println("Validate-->前置通知,方法名:" + name + ",参数:" + s);
}
}
3 配置文件
<context:component-scan base-package="pers.tianyu.proxyxml">
</context:component-scan>
<aop:config>
<!--设置一个公共的切入点表达式-->
<aop:pointcut id="pointCut" expression="execution(public int pers.tianyu.proxyxml.Calculator.*(..))"/>
<!--将IOC容器中的某个bean设置切面 -->
<aop:aspect ref="logAspect">
<aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before>
<aop:after method="afterMethod" pointcut-ref="pointCut"></aop:after>
<aop:after-returning method="afterReturningMethod" returning="result"
pointcut-ref="pointCut"></aop:after-returning>
<aop:after-throwing method="afterThrowingMehtod" throwing="ex" pointcut-ref="pointCut"></aop:after-throwing>
<aop:around method="aroundMethod" pointcut-ref="pointCut"></aop:around>
</aop:aspect>
<!--将validateAspect设置切切面,order设置优先级-->
<aop:aspect ref="validateAspect" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointCut"></aop:before>
</aop:aspect>
</aop:config>
4 测试
@Test
public void test() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
// 使用接口类型
Calculator bean = ioc.getBean(Calculator.class);
bean.add(1, 1);
}