一. 回顾
前面讲到了使用注解配置AOP,今天讲述使用XML方式配置AOP
本文章项目源码已经上传到本博客的“资源”处,有需要可前往免费下载
二. 知识储备
使用XML方式,所以要把某些注释删掉,比如@Aspect。
三. 例子
使用XML方式配置aop,实现日志功能。
ArithmeticCalculator.java
package com.atguigu.spring.aop.aspectJ_xml;
/**
* 算数计算器
* @author user
*
*/
public interface ArithmeticCalculator {
/**
* 加法
* @param i
* @param j
* @return
*/
public int add(int i, int j);
/**
* 减法
* @param i
* @param j
* @return
*/
public int sub(int i, int j);
/**
* 乘法
* @param i
* @param j
* @return
*/
public int mul(int i, int j);
/**
* 除法
* @param i
* @param j
* @return
*/
public int div(int i, int j);
}
ArithmeticCalculatorImpl.java
package com.atguigu.spring.aop.aspectJ_xml;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
// TODO Auto-generated method stub
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
// TODO Auto-generated method stub
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
// TODO Auto-generated method stub
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
// TODO Auto-generated method stub
int result = i / j;
return result;
}
}
LoggingAspect.java
package com.atguigu.spring.aop.aspectJ_xml;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* 切面:日志切面
* @author user
*
*/
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint) {
//方法名
String methodName = joinPoint.getSignature().getName();
//方法的参数列表
Object[] args = joinPoint.getArgs();
System.out.println("LoggingAspect==> The method " + methodName + " begin with : " + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("LoggingAspect==> The method " + methodName + " ends.");
}
public void afterThrowingMethod(JoinPoint joinPoint, Exception ex) {
//获取方法名
String methodName = joinPoint.getSignature().getName();
System.out.println("LoggingAspect==> The method " + methodName + " occurs Exception: " + ex);
}
public void afterReturningMethod(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("LoggingAspect==> The method " + methodName + "ends with: " + result);
}
public Object aroundMethod(ProceedingJoinPoint pjp) {
try {
//前置
//调用目标方法
Object result = pjp.proceed();
//返回
return result;
} catch (Throwable e) {
// TODO Auto-generated catch block
//异常
e.printStackTrace();
}finally {
//后置
}
return null;
}
}
ValidationAspect.java
package com.atguigu.spring.aop.aspectJ_xml;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
/**
* 验证切面
* @author user
*
*/
public class ValidationAspect {
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("ValidationAspect==> The method " + methodName + " begin with : " + Arrays.asList(args));
}
}
Main.java
package com.atguigu.spring.aop.aspectJ_xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("spring-aop_xml.xml");
ArithmeticCalculator ac =
ctx.getBean("arithmeticCalculatorImpl",ArithmeticCalculator.class);
System.out.println("ac: " + ac.getClass().getName());
int result = ac.add(1, 1);
System.out.println("Main===>: " +result );
System.out.println("============================================================='");
}
}
注意:=如果报错,那么99%都是xml里面的bean配置写错了,因为只要有一个bean配置不成功,都会报错。