12-spring声明式实现AOP

步骤:

  1. 添加相关依赖
  2. 编写代理接口
  3. 被代理接口实现类
  4. 编写切面类
  5. 配置方式实现aop
  6. 测试

1.pom.xml添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-12</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <!--导入AOP包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.aopalliance</groupId>
            <artifactId>com.springsource.org.aopalliance</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.cglib</groupId>
            <artifactId>com.springsource.net.sf.cglib</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2. POJO

package com.qwy.bean;

/**
 * @author qwy
 * @create 2021-04-25 14:12
 **/
public class Users {
}

3. 接口

package com.qwy.service;

import com.qwy.bean.Users;

/**
 * @author qwy
 * @create 2021-04-25 14:13
 **/
public interface UsersService {
    public void save(Users users);
    public void update(Users users);
    public void deleteUsersById(Integer id);
    public Users getUsersById(Integer id);
}

4.接口实现类

package com.qwy.service.impl;

import com.qwy.bean.Users;
import com.qwy.service.UsersService;

/**
 * @author qwy
 * @create 2021-04-25 14:15
 **/
public class UsersServiceImpl implements UsersService {
    public void save(Users users) {
        System.out.println("UsersServiceImpl.save");
    }

    public void update(Users users) {
        System.out.println("UsersServiceImpl.update");
    }

    public void deleteUsersById(Integer id) {
        System.out.println("UsersServiceImpl.deleteUsersById");
    }

    public Users getUsersById(Integer id) {
        System.out.println("UsersServiceImpl.getUsersById");
        return new Users();
    }
}

5. 切面类

package com.qwy.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author qwy
 * @create 2021-04-24 9:29
 **/

public class LogAspect {

    public void beforeMethod(JoinPoint joinPoint){
        //获取被代理对象的方法名
        String name = joinPoint.getSignature().getName();
        //获取被代理对象的参数列表
        Object[] args = joinPoint.getArgs();
        System.out.println("前置通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
    }

    public void afterReturnMethod(JoinPoint joinPoint,Object result){
        //获取被代理对象的方法名
        String name = joinPoint.getSignature().getName();
        //获取被代理对象的参数列表
        Object[] args = joinPoint.getArgs();
        System.out.println("返回通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
    }
    public void afterThrowMethod(JoinPoint joinPoint, Exception e){
        //获取被代理对象的方法名
        String name = joinPoint.getSignature().getName();
        //获取被代理对象的参数列表
        Object[] args = joinPoint.getArgs();
        System.out.println("异常通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
    }
    public void after(JoinPoint joinPoint){
        //获取被代理对象的方法名
        String name = joinPoint.getSignature().getName();
        //获取被代理对象的参数列表
        Object[] args = joinPoint.getArgs();
        System.out.println("后置通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
    }
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed=null;
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        try{
            //前置通知

            System.out.println("前置通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
            proceed = joinPoint.proceed();
            //返回通知
            System.out.println("返回通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
        }catch (Exception e){
            System.out.println("异常通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
            e.printStackTrace();
        }finally {
            System.out.println("后置通知:  被代理对象的方法:  " + name +" , 参数 :" + args);
        }

        return proceed;
    }

}

6. spring配置文件中配置AOP

<?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: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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--配置切面bean-->
    <bean id="logAspect" class="com.qwy.aspect.LogAspect"></bean>
    <!--配置业务bean-->
    <bean id="usersService" class="com.qwy.service.impl.UsersServiceImpl" />
    <!--配置AOP-->
    <aop:config>
        <!--定义切面-->
        <aop:aspect ref="logAspect" id="myaop">
            <!--配置切面表达式-->
           <aop:pointcut id="mycut" expression="execution(* com.qwy.service.impl.*.update(..))"/>
            <!--前置通知-->
               <aop:before method="beforeMethod" pointcut-ref="mycut"/>
            <!--返回通知-->
              <aop:after-returning method="afterReturnMethod" pointcut-ref="mycut" returning="result"/>
            <!--异常通知-->
              <aop:after-throwing method="afterThrowMethod" pointcut-ref="mycut" throwing="e"/>
            <!--后置通知-->
              <aop:after method="after" pointcut-ref="mycut"/>
            <!--环绕通知-->
            <!--<aop:around method="round" pointcut-ref="mycut"/>-->

        </aop:aspect>
    </aop:config>
</beans>

7. 测试类

package com.qwy.test;

import com.qwy.bean.Users;
import com.qwy.service.UsersService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author qwy
 * @create 2021-04-25 14:28
 **/
public class TestXMLAOP {
    private ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
    @Test
    public void test1(){
        UsersService usersService = ac.getBean("usersService", UsersService.class);
        usersService.save(new Users());
        usersService.update(new Users());
        usersService.deleteUsersById(1);
        usersService.getUsersById(1);
    }
}

执行结果:
UsersServiceImpl.save
前置通知: 被代理对象的方法: update , 参数 :[Ljava.lang.Object;@68999068
UsersServiceImpl.update
返回通知: 被代理对象的方法: update , 参数 :[Ljava.lang.Object;@68999068
后置通知: 被代理对象的方法: update , 参数 :[Ljava.lang.Object;@68999068
UsersServiceImpl.deleteUsersById
UsersServiceImpl.getUsersById

这里只是为了演示步骤,只对对update方法进行了增强。

上一篇:基于XML的方式实现AOP


下一篇:【项目笔记】AOP在项目中打日志的应用