20210325笔记

1. Spring AOP.

AOP: 面向切面编程  在不影响核心代码的前提下,可以在任意位置添加非核心代码。

2. AOP的前身

3. 通过动态代理实现核心业务和非核心业务的一种抽取。

比较麻烦

4. 可以使用spring 的aop来完成代理

(1) 把相关spring的依赖加入

   <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

        <!--切面依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>
    </dependencies>

(2)创建一个切面类

package com.ykq.aop.after;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author 闫克起
 * @Date 2021/3/25 15:52
 * @Version 1.0
 *
 * 日志切面类
 */
@Component
@Aspect //表示该类为切面类   -----> 删除某一个记录 添加日志。  操作日志
public class LogAspect {


    @Before(value = "execution(* com.aop.after.*.*(..))")  //service
    public void before(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("AAA--->The "+name+" method begin with "+ Arrays.asList(args));
    }

    @After(value = "execution(* com.aop.after.*.*(..))")  //后置通知 finally
    public void after(JoinPoint joinPoint){
        System.out.println("该方法总会被执行");
    }
    @AfterReturning(value = "execution(* com.aop.after.*.*(..))",returning = "r")     //后置返回通知  他可以得到返回结果。
    public void afterReturning(JoinPoint joinPoint,int r){
        String name = joinPoint.getSignature().getName();
        System.out.println("BBB--->The "+name+" method ends: "+r);
    }

    //catch(Exception e)
    @AfterThrowing(value = "execution(* com.aop.after.*.*(..))",throwing = "e")   //异常通知 只有发生异常时才会被执行。
    public void afterThrowing(Exception e){
        System.out.println(e.getMessage());
    }
}

(3) 配置文件开启切面注解

<?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:mvc="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 https://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.aop.after"/>

    <!--开启切面注解-->
    <mvc:aspectj-autoproxy />
</beans>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值