Spring---初识AOP

AOP:面向切面(方面)编程
通俗描述:不修改源代码方式,在主干功能里面添加新功能

底层原理:

AOP底层使用了动态代理

  • 1,有接口情况,使用JDK动态代理:创建接口实现类代理对象,增强类方法
  • 2,没有接口情况,使用CGLIB动态代理:创建子类的代理对象,增强类方法

如果对动态代理不熟悉,可以开该文章,点击链接

AOP术语:

  • 1,连接点

类里面的哪些方法可以被增强,就被称为连接点

  • 2,切入点

实际被真正增强的方法,称为切入点

  • 3,通知(增强)

通知的五种类型:
1,前置通知
2,后置通知
3,环绕通知
4,异常通知
5,最终通知

  • 4,切面

把通知应用到切入点的过程

AOP操作:

1,Spring框架一般都是基于AspectJ实现AOP操作

  • 1,Spring框架一般都是基于AspectJ实现AOP操作
  • 2,基于AspectJ实现AOP操作

什么是AspectJ?

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

2,基于AspectJ实现AOP操作

  • 1,基于xml配置文件实现(在实际开发中,基本不会用到,该文章没有这方面内容)
  • 2,基于注解方式实现

练习实践

AOP操作—基于注解:

步骤:

1,创建类,在类里面定义方法
2,创建增强类(编写增强逻辑)
  • 在增强类里面,创建方法,让不同 方法代表不同通知类型
3,进行通知配置
  • 1,在spring配置文件中,开启注解扫描
  • 2,使用注解创建被增强类和增强类的对象
  • 3,在增强类上面添加注解 @Aspect
  • 4,在spring配置文件中开启代理生成对象

准备操作:

因为是Spring和AspectJ一起去完成AOP操作,所以需要导入AspectJ的jar包。
下载后包太大了,但是我们只需要里面的aspectjtool.jar,所以在lib下解压后得到即可。

切入点表达式:

作用:知道哪个类里面的哪个方法进行增强

语法结构:

execution([权限修饰符] [返回类型][类全路径][方法名称]([参数列表]))
举例:
execution(* com.Abc.add(..))
*:表示全部数据类型,也可以用于包名,类名
..:直接使用方法参数

创建被增强类和增强类

被增强类;

@Component
public class User {

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

}

增强类:

@Component
@Aspect
public class UserPlus {

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

    //after注解表示作为后置通知(最终通知)
    @After(value="execution(* two.User.add())")
    public void after(){
        System.out.println("after");
    }



    //返回通知
    @AfterReturning(value="execution(* two.User.add())")
    public void AfterReturning(){
        System.out.println("AfterReturning");
    }


    //异常通知
    @AfterThrowing(value="execution(* two.User.add())")
    public void AfterThrowing(){
        System.out.println("AfterThrowing");
    }


    //环绕通知
    @Around(value="execution(* two.User.add())")
    public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("Around before");

        proceedingJoinPoint.proceed();

        System.out.println("Around after");
    }
    
}

Spring配置文件:

里面增加了个开启生成代理对象的标签,扫描的内容不知道可以看前面的IOC文章。还有就是aop名称空间

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


    <!--扫描-->
    <context:component-scan base-package="two"></context:component-scan>

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

扩张内容

优先级:

对于多个增强类对同一个类方法的增强,可以设置增强类的优先级

注解:@Order(数字值) :该值越小优先级越高

相同切入点的提取

如果多个方法都是对于同一个类同一个方法进行增强,那可以增加一个Pointcut注解,像以下这么写:

    @Pointcut(value="execution(* two.User.add())")
    public void pointDemo(){
        
    }

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

    //after注解表示作为后置通知(最终通知)
    @After(value="pointDemo()")
    public void after(){
        System.out.println("after");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值