spring基础aop切面编程注解@配置方式

68 篇文章 0 订阅
本文介绍了在Maven项目中使用SpringMVC框架,并结合AspectJ实现面向切面编程(AOP),包括定义接口、实现类、切点表达式和各种通知类型如前置、后置、环绕和异常通知。
摘要由CSDN通过智能技术生成

新建maven项目

 导入依赖

        <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 16:06
 */
public interface UserDao
{
    public void addUser();

    public void deleteUser();
}

UserDaoImpl.java

package com.shrimpking.aspectj;

import org.springframework.stereotype.Repository;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 16:07
 */
@Repository
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.annotation;

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;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/7/5 16:08
 */
@Aspect
@Component
public class MyAspect
{
    //切入点
    @Pointcut("execution(* com.shrimpking.aspectj.*.*(..))")
    public void myPointCut(){}

    //前置通知
    @Before("myPointCut()")
    public void myBefore(JoinPoint joinPoint)
    {
        System.out.println("前置通知,模拟权限检查...");
        System.out.println("目标类是," + joinPoint.getTarget());
        System.out.println("被植入增强的目标方法是," + joinPoint.getSignature().getName());
    }

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

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

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

    //最终通知
    @After("myPointCut()")
    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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 包扫描   -->
    <context:component-scan base-package="com.shrimpking.aspectj"/>
    <!-- 启动基于注解的声明式aspect支持   -->
    <aop:aspectj-autoproxy/>
</beans>

AnnotationTest.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 16:31
 */
public class AnnotationTest
{
    @Test
    public void test()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = context.getBean("userDaoImpl", UserDao.class);
        userDao.addUser();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值