Spring Aop注解实现

 更新时间:2021年07月16日 14:33:44   作者:宁在春  
本文我们通过Spring AOP和Java的自定义注解来实现日志的插入功能,非常不错,具有一定的参考借鉴价值,需要的朋友一起看看吧,希望对你有所帮助

Spring-aop-理论知识 Spring-Aop-注解实现 项目结构图

在这里插入图片描述

具体步骤:

1、创建maven 项目 导入依赖 创建好项目结构

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

2、写一个接口 及 其实现类

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:26
 */
public interface TestDao {
    public void delete();
}
/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:27
 */
@Service
public class TestDaoImpl implements TestDao {
    public void delete() {
        System.out.println("正在执行的方法-->删除");
    }
}

3、切面类

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-10 18:04
 */
@Aspect
@Component
public class MyAspectAnnotation {
    @Pointcut("execution(* com.dao.*.*(..))")
    private void myPointCut(){
    }
    /**
     * 前置通知 使用JoinPoint 接口作为参数获得目标对象的信息
     *    @Before("myPointCut()") ==
     *    <aop:after method="after" pointcut-ref="myPointCut"/>
     **/
    @Before("myPointCut()")
    public void before(JoinPoint jp){
        System.out.print("前置通知:模拟权限控制");
        System.out.println("目标对象:"+jp.getTarget()+",被增强的方法:"+jp.getSignature().getName());
    }
    @AfterReturning("myPointCut()")
    public void afterReturning(JoinPoint jp){
        System.out.print("后置返回通知:"+"模拟删除临时文件");
        System.out.println(",被增强的方法"+jp.getSignature().getName());
    }
    @Around("myPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕开始:执行目标方法前,模拟开启事务");
        Object obj = pjp.proceed();
        System.out.println("环绕结束:执行目标方法后,模拟关闭事务");
        return obj;
    }
    @AfterThrowing(value = "myPointCut()",throwing = "throwable")
    public void except(Throwable throwable){
        System.out.println("异常通知:"+"程序执行异常"+throwable.getMessage());
    }

    @After("myPointCut()")
    public void after(){
        System.out.println("最终通知:释放资源");
    }

}

4、application.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 https://www.springframework.org/schema/aop/spring-aop.xsd">
        <context:component-scan base-package="com.dao"/>
        <context:component-scan base-package="com.aspect"/>
        <!--启动基于注解的AspectJ支持-->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

测试

    @Test
    public void Test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
        testDao.delete();
        /**
         * 输出:
         * 环绕开始:执行目标方法前,模拟开启事务
         * 前置通知:模拟权限控制目标对象:com.dao.TestDaoImpl@2bef51f2,被增强的方法:delete
         * 正在执行的方法-->删除
         * 后置返回通知:模拟删除临时文件,被增强的方法delete
         * 最终通知:释放资源
         * 环绕结束:执行目标方法后,模拟关闭事务
         */
    }

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

  • IntelliJ IDEA进行中文汉化的详细教程(附图文讲解)

    IntelliJ IDEA进行中文汉化的详细教程(附图文讲解)

    今天为大家带来的是 IntelliJ IDEA 中文汉化教程以及中文插件包下载教程,经常收到小伙伴在后台给我留言,问 IDEA 怎么进行中文汉化,因为很多小伙伴是刚入门 Java,看到 IDEA 菜单全英文有些不太适应,需要的朋友可以参考下
    2024-12-12
  • SpringBoot如何对LocalDateTime进行格式化并解析

    SpringBoot如何对LocalDateTime进行格式化并解析

    这篇文章主要介绍了SpringBoot如何对LocalDateTime进行格式化方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Java servlet、filter、listener、interceptor之间的区别和联系

    Java servlet、filter、listener、interceptor之间的区别和联系

    这篇文章主要介绍了Java servlet、filter、listener、interceptor之间的区别和联系的相关资料,需要的朋友可以参考下
    2016-11-11
  • 几种常见mybatis分页的实现方式

    几种常见mybatis分页的实现方式

    这篇文章主要介绍了几种常见mybatis分页的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Flink部署集群整体架构源码分析

    Flink部署集群整体架构源码分析

    这篇文章主要为大家介绍了Flink部署集群及整体架构示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • myeclipse安装Spring Tool Suite(STS)插件的方法步骤

    myeclipse安装Spring Tool Suite(STS)插件的方法步骤

    这篇文章主要介绍了myeclipse安装Spring Tool Suite(STS)插件的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Java List简介_动力节点Java学院整理

    Java List简介_动力节点Java学院整理

    Java中可变数组的原理就是不断的创建新的数组,将原数组加到新的数组中,下文对Java List用法做了详解。需要的朋友参考下吧
    2017-05-05
  • Collection stream使用示例详解

    Collection stream使用示例详解

    这篇文章主要介绍了Collection stream使用示例,stream流几乎可以完成对集合的任意操作,映射、去重、分组、排序、过滤等
    2022-12-12
  • Java中final关键字详解及实例

    Java中final关键字详解及实例

    这篇文章主要介绍了Java中final关键字详解及实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • 详解eclipse项目中.classpath文件的使用

    详解eclipse项目中.classpath文件的使用

    这篇文章主要介绍了详解eclipse项目中.classpath文件的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10

最新评论