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);
- }
- }