spring5入门----AOP操作

前言

一、AOP的操作(准备工作)

1、Spring 框架一般都是基于 AspectJ 实现 AOP 操做

(1)AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使
用,进行 AOP 操作

2、基于 AspectJ 实现 AOP 操作

(1)基于 xml 配置文件实现
(2)基于注解方式实现(使用)

3、在项目工程里面引入 AOP 相关依

在这里插入图片描述

4、切入点表达式

(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称] [参数列表]) )
举例 1:对 com.atguigu.dao.BookDao 类里面的 add 进行增强
execution(* com.atguigu.dao.BookDao.add(…))
举例 2:对 com.atguigu.dao.BookDao 类里面的所有的方法进行增强
execution(* com.atguigu.dao.BookDao.* (…))
举例 3:对 com.atguigu.dao 包里面所有类,类里面所有方法进行增强
execution(* com.atguigu.dao.. (…))

二、AOP 操作(AspectJ 注解)

1、创建类,在类里面定义方法

public class User {
 public void add() {
 System.out.println("add.......");
 }
}

2、创建增强类(编写增强逻辑)

(1)在增强类里面,创建方法,让不同方法代表不同通知类型

//增强的类
public class UserProxy {
 public void before() {//前置通知
 System.out.println("before......");
 }
}

3、进行通知的配置

(1)在 spring 配置文件中,开启注解扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.NS"/>
</beans>

(2)使用注解创建 User 和 UserProxy 对象

@Component//实现bean实例
public class User {
    public void add(){
        System.out.println("add......");
    }
}

@Component//实现bean类

public class UserProxy

(3)在增强类上面添加注解 @Aspect

@Aspect//生成代理对象----UserProxy是代理对象
@Component//实现bean类
public class UserProxy

(4)在 spring 配置文件中开启生成代理对象

  <!-- 开启 Aspect 生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4、配置不同类型的通知

(1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置

package com.NS.aopanno;
//@Order(3)//设置增加类优先级(数值越小 级别越高)---同时增加同一个方法
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect//生成代理对象----UserProxy是代理对象
@Component//实现bean类
public class UserProxy {

    //相同的切入点的抽取
    /** 切入点表达式作用:知道对哪个类里面的哪个方法进行增强*/
    @Pointcut(value = "execution(* com.NS.aopanno.User.add(..))")
    public void pointdemo(){
    }

    //    @Before() 注解表示作为前置通知
    @Before(value = "pointdemo()")
    public void before(){
        System.out.println("before.....");
    }

    //  @AfterThrowing 异常通知
    @AfterThrowing(value ="pointdemo()" )
    public void afterThrowing(){
        System.out.println("afterThrowing....");
    }

    //  @After() 注解表示作为后置通知
    @After(value = "pointdemo()")
    public void after(){
        System.out.println("after.....");
    }

    //@AfterReturning 注解表示作为最终通知
    @AfterReturning(value = "pointdemo()")
    public void afterReturning(){
        System.out.println("afterReturning.....");
    }

    //@Around  注解表示围绕通知
    @Around(value = "execution(* com.NS.aopanno.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前.....");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后.....");
    }
    
}

在这里插入图片描述

5、有多个增强类多同一个方法进行增强,设置增强类优先级

(1)在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越

@Component
@Aspect
@Order(1)
public class PersonProxy

6、完全使用注解开发

(1)创建配置类,不需要创建 xml 配置文件

package com.NS.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan(basePackages = {"com.NS"})//开启注解扫描
@EnableAspectJAutoProxy(proxyTargetClass =true)//开启开启 Aspect 生成代理对象
public class ConfigAop {

}

三、AOP 操作(AspectJ 配置文件

1、创建两个类,增强类和被增强类,创建方法

Book类

package com.NS.apoxml;
import org.springframework.stereotype.Component;

@Component
public class Book {
    public void buy(){
        System.out.println("buy......");
    }
}

BookProxy类 Book类的代理类

package com.NS.apoxml;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class BookProxy {
    public void  before(){
        System.out.println("before......");
    }
}

2、在 spring 配置文件中创建两个类对象

  <bean id="book" class="com.NS.apoxml.Book"></bean>
    <bean id="bookProxy" class="com.NS.apoxml.BookProxy"></bean>

3、在 spring 配置文件中配置切入点

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">


    <!--创建对象-->
    <bean id="book" class="com.NS.apoxml.Book"></bean>
    <bean id="bookProxy" class="com.NS.apoxml.BookProxy"></bean>

    <!--配置文件-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="p" expression="execution(* com.NS.apoxml.Book.buy(..))"/>

        <!--配置切面-->
        <aop:aspect ref="bookProxy">

            <!--增强作用在具体方法-->
            <aop:before method="before" pointcut-ref="p"/>

        </aop:aspect>

    </aop:config>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值