java中写了一个接口以及它的实现类,稍后就拦截这些方法
- package myspring.calculator;
- public interface IArithmeticCalculator {
- public double add(double a, double b);
- public double sub(double a, double b);
- public double mul(double a, double b);
- public double div(double a, double b);
- }
- package myspring.calculator;
- public class ArithmeticCalculatorImp implements IArithmeticCalculator {
- public double add(double a, double b) {
- double result = a + b;
- System.out.println(a + " + " + b + " = " + result);
- return result;
- }
- public double sub(double a, double b) {
- double result = a - b;
- System.out.println(a + " - " + b + " = " + result);
- return result;
- }
- public double mul(double a, double b) {
- double result = a * b;
- System.out.println(a + " * " + b + " = " + result);
- return result;
- }
- public double div(double a, double b) {
- if (b == 0) {
- throw new IllegalArgumentException("Division by zero");
- }
- double result = a / b;
- System.out.println(a + " / " + b + " = " + result);
- return result;
- }
- }
aop.xml如下,先用ioc将接口与实现类建立关系
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- ">
- <aop:aspectj-autoproxy />
- <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp"></bean>
- <bean class="myspring.aop.CalculatorLogAspect" />
- </beans>
在上面可以看到这句话:<bean class="myspring.aop.CalculatorLogAspect" /> ,这表示当调用arithmeticCalculator这个bean里面的方法的时候,要按照CalculatorLogAspect里的拦截配置进行拦截。
myspring.aop.CalculatorLogAspect如下:
- package myspring.aop;
- 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;
- @Aspect
- public class CalculatorLogAspect {
- private MyLogger logger = new MyLogger();//随便写的一个记录日志的类,只是向控制台打印信息
- @Pointcut("execution(* *.add(..))")
- public void logAdd(){}
- @Before("logAdd()")//表示按照logAdd方法上标示的拦截表达式来拦截add方法
- public void logBefore(JoinPoint joinPoint)
- {
- logger.log("{Before} The method: "+
- joinPoint.getSignature().getName()+
- " args:"+Arrays.toString(joinPoint.getArgs()));
- }
- @AfterReturning(
- pointcut="execution(* *.*(..))", //表示所有方法return之后都要经过此拦截
- returning="result")
- public void logAfterReturning(JoinPoint joinPoint, Object result)
- {
- logger.log("{AfterReturning} method:"+joinPoint.getSignature().getName()
- + " result:"+result);
- }
- @AfterThrowing(
- pointcut="execution(* *.*(..))", //表示所有方法异常之后都要经过此拦截
- throwing="e")
- public void logAfterThrowing(JoinPoint joinPoint, IllegalArgumentException e)
- {
- logger.log("{AfterThrowing} method:"+
- joinPoint.getSignature().getName()+
- " Exception:"+e.getMessage()
- );
- }
- @After("execution(* *.*(..))") //表示所有方法执行完毕之后都要经过此拦截
- public void logAfter(JoinPoint joinPoint)
- {
- logger.log("{After} method:"+joinPoint.getSignature().getName());
- }
- @Around("execution(* *.*(..))") //表示所有方法运行之前与之后都要经过此拦截
- public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable
- {
- logger.log("{Around}Before Method:"+joinPoint.getSignature().getName());
- try
- {
- //调用目标方法
- Object result = joinPoint.proceed();
- logger.log("{Around}After Method:"+
- joinPoint.getSignature().getName()+
- " result:"+result);
- //返回目标方法的返回值
- return result;
- }
- catch(IllegalArgumentException e)
- {
- logger.log("{Around}Exception:Illegal Argument "+
- Arrays.toString(joinPoint.getArgs()) );
- throw e;
- }
- }
- }
logger类如下:
- package myspring.aop;
- public class MyLogger {
- public void log(String info)
- {
- System.out.println("[Logger] "+info);
- }
- }
编写测试类进行测试:
- package myspring.test;
- import myspring.calculator.IArithmeticCalculator;
- import myspring.calculator.IMaxCalculator;
- import myspring.calculator.IMinCalculator;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestAOP {
- //AspectJ方式
- @Test
- public void test1()
- {
- ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml");
- IArithmeticCalculator cal = (IArithmeticCalculator)ac.getBean("arithmeticCalculator");
- cal.add(2, 3);
- cal.sub(5, 3);
- cal.mul(3, 4);
- cal.div(6, 0);
- }
- }