• Spring,AOP,基于注解声明式ApsectJ


    本篇文章,类似于:Spring深入浅出(十三),AOP,AspectJ,基于注解开发,只是从更加全面的角度看注解。注解的初步知识,请参考上文。

    一、创建接口类

    package com.itheima.jdk;
    
    public interface UserDao {
        public void addUser();
    
        public void deleteUser();
    }

    二、创建实现类

    注意:需要添加@Repository("userDao"),以使得component-scan有效。

    package com.itheima.jdk;
    
    import org.springframework.stereotype.Repository;
    
    @Repository("userDao")
    public class UserDaoImpl implements UserDao {
        public void addUser() {
            System.out.println("添加用户");
        }
    
        public void deleteUser() {
            System.out.println("删除用户");
        }
    }

    三、创建切面类

    package com.itheima.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; /** * 切面类,在此类中编写通知 */ @Aspect @Component public class MyAspect { // 定义切入点表达式 @Pointcut("execution(* com.itheima.jdk.*.*(..))") // 使用一个返回值为void、方法体为空的方法来命名切入点 private void myPointCut(){} // 前置通知 @Before("myPointCut()") public void myBefore(JoinPoint joinPoint) { System.out.print("前置通知 :模拟执行权限检查...,"); System.out.print("目标类是:"+joinPoint.getTarget() ); System.out.println(",被织入增强处理的目标方法为:" +joinPoint.getSignature().getName()); } // 后置通知 @AfterReturning(value="myPointCut()") public void myAfterReturning(JoinPoint joinPoint) { System.out.print("后置通知:模拟记录日志...," ); System.out.println("被织入增强处理的目标方法为:" + joinPoint.getSignature().getName()); } // 环绕通知 @Around("myPointCut()") public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { // 开始 System.out.println("环绕开始:执行目标方法之前,模拟开启事务..."); // 执行当前目标方法 Object obj = proceedingJoinPoint.proceed(); // 结束 System.out.println("环绕结束:执行目标方法之后,模拟关闭事务..."); return obj; } // 异常通知 @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("最终通知:模拟方法结束后的释放资源..."); } }

    四、创建配置文件

    1. <context:component-scan>元素设置了需要扫描的包,使得注解生效。

    2. <aop:aspectj-autoproxy />来启动Spring对基于注解声明式ApsectJ的支持。

    <?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"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
          <!-- 指定需要扫描的包,使注解生效 -->
          <context:component-scan base-package="com.itheima.jdk" />
          <context:component-scan base-package="com.itheima.aspectj.annotation" />
          <!-- 启动基于注解的声明式AspectJ支持 -->
          <aop:aspectj-autoproxy />
    </beans>

    五、创建主程序

    package com.itheima.aspectj.annotation;
    import org.springframework.context.ApplicationContext;
    import 
        org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.itheima.jdk.UserDao;
    // 测试类
    public class TestAnnotationAspectj {
        public static void main(String args[]) {
            String xmlPath = 
                      "com/itheima/aspectj/annotation/applicationContext.xml";
            ApplicationContext applicationContext = 
                     new ClassPathXmlApplicationContext(xmlPath);
            // 1 从spring容器获得内容
            UserDao userDao = (UserDao) applicationContext.getBean("userDao");
            // 2 执行方法
            userDao.addUser();
        }
    }

    六、运行结果

    环绕开始:执行目标方法之前,模拟开启事务...
    前置通知 :模拟执行权限检查...,目标类是:com.itheima.jdk.UserDaoImpl@68e965f5,被织入增强处理的目标方法为:addUser
    添加用户
    环绕结束:执行目标方法之后,模拟关闭事务...
    最终通知:模拟方法结束后的释放资源...
    后置通知:模拟记录日志...,被织入增强处理的目标方法为:addUser

     本文参考:《Java EE企业级应用开发教程》

  • 相关阅读:
    树莓派4b 对于 Failed to execute command 的解决方案
    Windows 10 启用以太网/公共网络共享
    树莓派 配置 OMV 搭建 NAS(四) 文件夹共享
    树莓派 配置 OMV 搭建 NAS(三) 硬盘挂载
    树莓派 配置 OMV 搭建 NAS(二) 配置 OMV 5
    树莓派 系统配置
    树莓派/Debian 网线连接主机
    树莓派 配置 OMV 5 搭建 NAS(一) 安装 OMV 5
    Vmware 15 虚拟机挂载硬盘
    p9半幺群
  • 原文地址:https://www.cnblogs.com/nayitian/p/15054812.html
Copyright © 2020-2023  润新知