zoukankan      html  css  js  c++  java
  • [刘阳Java]_Spring AOP注解详细介绍_第8讲

    这节内容非常关键,我们会比较详细地介绍Spring AOP注解的使用

    1. 要使用Spring AOP注解,必须满足如下的事项

    • 导入Aspectj的jar、Spring3.0-AOP.jar、aopalliance.jar
    • 需要在配置文件中加入注解的配置,例如:bean-aop-annotiation.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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>

    如果使用Spring AOP注解,最好的用处就是减少在配置文件中的AOP内容。但是如果要掌握好Spring的AOP还需要学习注解的语法,下面的内容会给大家慢慢介绍

    2. 织入点语法

    package com.spring.aop;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    @Aspect
    public class LogAop {
    
    @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))")
     public void logBefore() {
        System.out.println("方法执行之前转载日志");
     }
    }

    execution(public void com.spring.dao.impl.StudentDaoImpl.*(..)),这个就是织入点的语法,它告诉AOP框架哪个类中方法需要进行AOP

    3. execution语法介绍

    • execution(public * *(..))
    • execution(* set*(..))
    • execution(* com.xyz.service.AccountService.*(..))
    • execution(* com.xyz.service..*.*(..))
    • 上面只是举例说明了execution的语法,下面是一个标准的语法定义
    execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

     4. Spring AOP注解例子

    • @Before前置建议,它是在执行一个业务方法之前插入的切面
    • @AfterReturning,它是当一个方法正常运行后,执行的切面
    • @After,它是当方法执行成功或者出现异常的时候都会执行切面
    • @Around,它相当于一个AOP链,如果当前AOP执行后,就让下一个AOP执行
    • @AfterThrowing,如果在方法中有错误抛出,则执行此建议
    package com.spring.aop;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    @Aspect
    public class LogAop {
    
    @Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))")
     public void logBefore() {
        System.out.println("方法执行之前转载日志");
     }
    
    @AfterReturning("execution(public void com.spring.dao.impl.StudentDaoImpl.insert(..))")
    public void logAfterReturning() {
        System.out.println("方法执行返回后载入日志");
    }
    
    @After("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))")
    public void logAfter() {
        System.out.println("Finally载入日志");
    }
    
    @Around("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))")
    public Object doBasicProfiling(ProceedingJoinPointpjp) throws Throwable {
        System.out.println("===around建议载入日志===" + new Date());
        Object o = pjp.proceed();
         return o;
    }
    
    @AfterThrowing("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))")
    public void logAfterThrowing() {
        System.out.println("===有参数异常载入日志===" + new Date());
    }
    }
  • 相关阅读:
    CV大牛/实验室主页
    mendeley使用技巧
    卷积理解与思考
    CMake构建OpenGL项目
    信号与系统学习(2)-跃阶信号
    信号与系统学习(1)-正弦信号和指数信号
    txt转换为mat
    matlab取整函数
    三维观察流水线的理解
    C#中文和UNICODE字符转换方法
  • 原文地址:https://www.cnblogs.com/liuyangjava/p/6678420.html
Copyright © 2011-2022 走看看