zoukankan      html  css  js  c++  java
  • Java Spring AOP用法

    Spring提供了两个核心功能,一个是IoC(控制反转),另外一个便是Aop(面向切面编程),IoC有助于应用对象之间的解耦,AOP则可以实现横切关注点(如日志、安全、缓存和事务管理)与他们所影响的对象之间的解耦。

    1.简介

    AOP主要包含了通知、切点和连接点等术语,介绍如下

    通知(Advice)
    通知定义了切面是什么以及何时调用,何时调用包含以下几种

    Before 在方法被调用之前调用通知
    After 在方法完成之后调用通知,无论方法执行是否成功
    After-returning 在方法成功执行之后调用通知
    After-throwing 在方法抛出异常后调用通知
    Around 通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为

    切点(PointCut)
    通知定义了切面的什么何时,切点定义了何处,切点的定义会匹配通知所要织入的一个或多个连接点,我们通常使用明确的类的方法名称来指定这些切点,或是利用正则表达式定义匹配的类和方法名称来指定这些切点。

    连接点(JoinPoint)
    连接点是在应用执行过程中能够插入切面的一个点,这个点可以是调用方法时,抛出异常时,甚至是修改一个字段时,切面代码可以利用这些连接点插入到应用的正常流程中,并添加新的行为,如日志、安全、事务、缓存等。

    2.用法

    同依赖注入一样,AOP在spring中有两种配置方式,一是xml配置的方式,二是自动注解的模式。

    这里使用注解的形式,配置的方式不喜欢,哈哈

    2.1 配置自动代理

    使用配置注解,首先我们要将切面在spring上下文中声明成自动代理bean,我们需要在Spring配置文件中配置如下一句话即可

    <!--启用Spring对@AspectJ的支持 -->
    <!-- 使用基于注解的方式配置切面,需要有下面这个配置 -->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

    当然我们需要在xml的根目录beans下引用aop的命名空间和xsi

    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"

    2.2使用@Aspect注解

    声明一个切面,只需要在类名上添加@Aspect属性即可,具体的连接点,我们用@Pointcut和@Before、@After等标注。具体如下
    在声明前 我们需要依赖配置pom

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.11</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.11</version>
    </dependency>

    声明切面类,包含了注解@Aspect以及何时(如@Before)执行通知

    @Aspect
    @Service
    public class XmlAopDemoUserLog {
    
        // 配置切点 及要传的参数   
        @Pointcut("execution(* com.winner.test(..)) ")
        public void pointCut() {
    
        }
    
        // 配置连接点 方法开始执行时通知
        @Before("pointCut()")
        public void beforeLog() {
            System.out.println("开始执行前置通知  日志记录:");
        }
    
        // 方法执行完后通知
        @After("pointCut()")
        public void afterLog() {
            System.out.println("开始执行后置通知 日志记录:");
        }
        
        // 执行成功后通知
        @AfterReturning("pointCut()")
        public void afterReturningLog() {
            System.out.println("方法成功执行后通知 日志记录:");
        }
    
        //    抛出异常后通知
        @AfterThrowing("pointCut()")
        public void afterThrowingLog() {
            System.out.println("方法抛出异常后执行通知 日志记录");
        }
    
        //    环绕通知
        @Around("pointCut()")
        public Object aroundLog(ProceedingJoinPoint joinpoint) {
            Object result = null;
            try {
                System.out.println("环绕通知开始 日志记录");
                long start = System.currentTimeMillis();
    
                //有返回参数 则需返回值
                result = joinpoint.proceed();
    
                long end = System.currentTimeMillis();
                System.out.println("总共执行时长" + (end - start) + " 毫秒");
                System.out.println("环绕通知结束 日志记录");
            } catch (Throwable t) {
                System.out.println("出现错误");
            }
            return result;
        }
    }

     

  • 相关阅读:
    Win7旗舰版中的IIS配置asp.net的运行环境
    jquery $(document).ready() 与window.onload的区别
    DEFAULT CURRENT_TIMESTAMP
    存储过程 跳出
    rabbitMQ 重试
    oracle update left join 写法
    error: snap "eclipse" has "install-snap" change in progress
    数据库去空格 去table 去回车符号 去重
    分组 拼接字段
    msyql 去重
  • 原文地址:https://www.cnblogs.com/winner-0715/p/6733954.html
Copyright © 2011-2022 走看看