SpringAOP理解

概念
AOP面向切面编程,可以实现对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可用性,同时提高开发效率。同时符合OCP开闭原则,即不通过修改源代码,也可以添加新的功能

底层原理
图解
在这里插入图片描述
JDK代理
在这里插入图片描述
在这里插入图片描述

CGLIB代理

AOP操作术语
连接点
切入点
通知
切面

在这里插入图片描述
在这里插入图片描述
操作
添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu.com</groupId>
    <artifactId>spring学习</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
<!--        &lt;!&ndash;spring框架是需要的依赖&ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>org.springframework</groupId>-->
<!--            <artifactId>spring-context</artifactId>-->
<!--            <version>5.2.6.RELEASE</version>-->
<!--        </dependency>-->

<!--        &lt;!&ndash;单元测试所需要的依赖&ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>junit</groupId>-->
<!--            <artifactId>junit</artifactId>-->
<!--            <version>4.12</version>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->

<!--        &lt;!&ndash; https://mvnrepository.com/artifact/com.alibaba/druid &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>com.alibaba</groupId>-->
<!--            <artifactId>druid</artifactId>-->
<!--            <version>1.1.10</version>-->
<!--        </dependency>-->

<!--        &lt;!&ndash; https://mvnrepository.com/artifact/org.springframework/spring-aop &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>org.springframework</groupId>-->
<!--            <artifactId>spring-aop</artifactId>-->
<!--            <version>5.3.8</version>-->
<!--        </dependency>-->

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter</artifactId>-->
<!--            <version>2.5.4</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

创建类

package com.atguigu.gk.anno;

import org.springframework.stereotype.Component;

/**
* 被增强类
*/
@Component
public class User {

    public void f(){
        System.out.println("f方法执行...");
    }
}

package com.atguigu.gk.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class UserProxy {

    //前置通知
    //@Before表示作为前置通知
    @Before(value = "execution(* com.atguigu.gk.anno.User.f(..))")
    public void before() {
        System.out.println("before...");
    }

    //后置通知
    @After(value = "execution(* com.atguigu.gk.anno.User.f(..))")
    public void after() {
        System.out.println("after...");
    }

    //环绕通知
    @Around(value = "execution(* com.atguigu.gk.anno.User.f(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前...");
        //被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后...");
    }

    @AfterThrowing(value = "execution(* com.atguigu.gk.anno.User.f(..))")
    public void afterThrowing() {
        System.out.println("before...");
    }

    //
    @AfterReturning(value = "execution(* com.atguigu.gk.anno.User.f(..))")
    public void afterReturning() {
        System.out.println("afterReturning...");
    }
}

创建配置文件

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


    <!--
       开启组件扫描:
           1 如果扫描多个包,多个包使用逗号隔开
           2 扫描包上层目录
       -->
    <context:component-scan base-package="com.atguigu.gk.anno"></context:component-scan>

    <!--开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.atguigu.gk.service;

import com.atguigu.gk.anno.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5 {

    @Test
    public void f1(){

        //1.实例化IOC容器并加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("anno.xml");

        User user = context.getBean("user", User.class);
        user.f();
    }
}

@Order
在这里插入图片描述

视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值