首页 > 代码库 > Spring初学之annotation实现AOP前置通知和后置通知

Spring初学之annotation实现AOP前置通知和后置通知

实现两个整数的加减乘除,并在每个计算前后打印出日志。

ArithmeticCalculator.java:

package spring.aop.impl;

public interface ArithmeticCalculator {
    
    int add(int i,int j);
    int sub(int i,int j);
    int mul(int i,int j);
    int div(int i,int j);
    
}

ArithmeticCalculatorImpl.java:

package spring.aop.impl;

import org.springframework.stereotype.Component;

@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    public int add(int i, int j) {
        int result = i+j;
        return result;
    }

    public int sub(int i, int j) {
        int result = i-j;
        return result;
    }

    public int mul(int i, int j) {
        int result = i*j;
        return result;
    }

    public int div(int i, int j) {
        int result = i/j;
        return result;
    }

}

LoggingAspect.java:

package spring.aop.impl;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//把这个类声明为一个切面:1.把该类放入到IOC容器中,2.再声明一个切面

@Aspect
@Component
public class LoggingAspect {

    //声明该方法是一个前置通知:在目标方法之前执行
    @Before("execution( public int spring.aop.impl.*.*(int,int) )")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        List<Object> args=Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+methodName+" begins"+args);
    }
    
    //后置通知:在目标方法执行后执行(无论是否发生异常),的通知。
    //在后置通知中不能访问目标方法的执行结果
    @After("execution( public int spring.aop.impl.*.*(int,int) )")
    public void afterMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        System.out.println("The method "+methodName+" ends");
    }
    
}

ApplicationContext.xml:

<?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.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="spring.aop.impl" />
        
    <!-- 使AspectJ注解起作用,自动为匹配的类生成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

测试:

package spring.aop.impl.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring.aop.impl.ArithmeticCalculator;

public class Main {
    public static void main(String[] args) {
        
        //1.创建spring的IOC容器
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从容器中获取bean的实例
        ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
        //3.使用bean
        int result = arithmeticCalculator.add(5, 8);
        System.out.println("result:"+result);
        
        result = arithmeticCalculator.div(5, 8);
        System.out.println("result:"+result);
    }
}

输出:

The method add begins[5, 8]
The method add ends
result:13
The method div begins[5, 8]
The method div ends
result:0

 

Spring初学之annotation实现AOP前置通知和后置通知