新建maven项目

spring基础aop切面编程XML配置方式_System

导入依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>

UserDao.java

package com.shrimpking.aspectj;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 14:53
 */
public interface UserDao
{
    public void addUser();

    public void deleteUser();
}

UserDaoImpl.java

package com.shrimpking.aspectj;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 14:54
 */
public class UserDaoImpl implements UserDao
{
    @Override
    public void addUser()
    {
        System.out.println("添加用户");
    }

    @Override
    public void deleteUser()
    {
        System.out.println("删除用户");
    }
}

MyAspect.java

package com.shrimpking.aspectj.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 14:55
 * 切面类,在此类中编写通知
 */
public class MyAspect
{
    //前置通知
    public void myBefore(JoinPoint joinPoint)
    {
        System.out.println("前置通知,模拟权限检查...");
        System.out.println("目标类是," + joinPoint.getTarget());
        System.out.println("被植入增强的目标方法是," + joinPoint.getSignature().getName());
    }

    //后置通知
    public void myAfterReturning(JoinPoint joinPoint)
    {
        System.out.println("后置通知,模拟日志记录...");
        System.out.println("被植入增强的目标方法是," + joinPoint.getSignature().getName());
    }

    //环绕通知
    public void myAround(ProceedingJoinPoint joinPoint) throws Throwable
    {
        System.out.println("环绕通知开始,执行目标方法前,模拟开启事务...");
        joinPoint.proceed();
        System.out.println("环绕通知结束,执行目标方法之后,模拟关闭事务...");
    }

    //异常通知
    public void myAfterThrowing(JoinPoint joinPoint,Throwable e)
    {
        System.out.println("异常通知,出错了" + e.getMessage());
    }

    //最终通知
    public void myAfter()
    {
        System.out.println("最终通知,模拟方法结束后释放资源...");
    }
}

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: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 id="userDaoImpl" class="com.shrimpking.aspectj.UserDaoImpl"/>

    <!-- 切面   -->
    <bean id="myAspect" class="com.shrimpking.aspectj.xml.MyAspect"/>
    
    <!--  aop配置  -->
    <aop:config>
        <aop:aspect id="aspect" ref="myAspect">
            <!--  切入点          -->
            <aop:pointcut id="myPointCut" expression="execution(* com.shrimpking.aspectj.*.*(..))"/>
            <!--  前置通知          -->
            <aop:before method="myBefore" pointcut-ref="myPointCut"/>
            <!--  后置通知          -->
            <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut"/>
            <!--  环绕通知          -->
            <aop:around method="myAround" pointcut-ref="myPointCut"/>
            <!--异常通知-->
            <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/>
            <!-- 最终通知           -->
            <aop:after method="myAfter" pointcut-ref="myPointCut"/>
        </aop:aspect>
    </aop:config>
    
</beans>

XmlTest.java

import com.shrimpking.aspectj.UserDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 15:19
 */
public class XmlTest
{
    @Test
    public void test()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = context.getBean("userDaoImpl", UserDao.class);
        userDao.addUser();

    }
}

spring基础aop切面编程XML配置方式_System_02