【Spring】核心部分之AOP:通过列举代码例子,从底层刨析,深入源码,轻轻松松理解Spring的核心AOP,AOP有这一篇足以

AOP

基本概念

面向切面编程,也叫面向方面编程,利用aop可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分之间降低耦合,提高程序的可重用性,同时提高了开发效率。

基本原理

用到了代理模式,有关代理模式见文章:设计模式——代理模式详解(Java版)

专业术语

连接点:可以被增强的方法
切入点:实际被增强的方法
通知(增强):实际增强的部分
通知有多种类型
前置通知
后置通知
环绕通知
异常通知
最终通知
切面:是一个动作,将通知应用到切入点的过程

案例演示

基于注解(重点)

1.需要增强的方法

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

2,编写增强类

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

3.进行通知的配置

第一步:在spring的配置文件中开启注解扫描

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
<!--开启注解扫描 -->
    <context:component-scan base-package="com.li.spring"></context:component-scan>

第二步:使用注解创建两个类的对象

@Component
public class User {
    public void Add(){
        System.out.println("add............");
    }
}
@Component
public class UserProxy {
    //&#x524D;&#x7F6E;&#x901A;&#x77E5;
    public void before(){
        System.out.println("before...........");
    }
}

第三步:在增强的类上面加注解Aspect

@Aspect //&#x751F;&#x6210;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;
@Component
public class UserProxy {
    //&#x524D;&#x7F6E;&#x901A;&#x77E5;
    public void before(){
        System.out.println("before...........");
    }
}

第四步:在spring的配置文件中开启Aspect生成代理对象

 xmlns:aop="http://www.springframework.org/schema/aop"
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  <!--开启aspect生成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4.配置不同类型的通知

@Component
@Aspect //&#x751F;&#x6210;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;
public class UserProxy {
    //&#x524D;&#x7F6E;&#x901A;&#x77E5;
    @Before(value = "execution(* com.li.spring.User.Add(..))")
    public void before(){
        System.out.println("before...........");
    }
    //&#x540E;&#x7F6E;&#x901A;&#x77E5;
    @After(value = "execution(* com.li.spring.User.Add(..))")
    public void after(){
        System.out.println("after...........");
    }
    @AfterReturning(value = "execution(* com.li.spring.User.Add(..))")
    public void AfterReturning(){
        System.out.println("AfterReturning...........");
    }
    @AfterThrowing(value = "execution(* com.li.spring.User.Add(..))")
    public void AfterThrowing(){
        System.out.println("AfterThrowing...........");
    }
    //&#x73AF;&#x7ED5;&#x901A;&#x77E5;
    @Around(value = "execution(* com.li.spring.User.Add(..))")
    public void Around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("&#x73AF;&#x7ED5;&#x4E4B;&#x524D;...........");
        try {
            Object proceed = proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        System.out.println("&#x73AF;&#x7ED5;&#x4E4B;&#x540E;...........");
    }
}

【Spring】核心部分之AOP:通过列举代码例子,从底层刨析,深入源码,轻轻松松理解Spring的核心AOP,AOP有这一篇足以

基于配置文件

1.准备要增强的类及方法

public class People {
    public void say(){
        System.out.println("say................");
    }
}
public class PeopleProxy {
    public void before(){
        System.out.println("before.................");
    }
}

2.在spring的配置文件中创建两个类

<bean id="people" class="com.li.spring.People"></bean>
    <bean id="peopleproxy" class="com.li.spring.PeopleProxy"></bean>

3.在spring的配置文件中配置切入点

<!--配置aop增强-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="p" expression="execution(* com.li.spring.People.say(..))">
        <!-- 配置切面-->
        <aop:aspect ref="peopleproxy">
            <!--配置增强作用在具体的方法上 -->
            <aop:before method="before" pointcut-ref="p"></aop:before>
        </aop:aspect>
    </aop:pointcut></aop:config>

4.测试

public class test {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml");
        People people = context.getBean("people", People.class);
        people.say();
    }
}

【Spring】核心部分之AOP:通过列举代码例子,从底层刨析,深入源码,轻轻松松理解Spring的核心AOP,AOP有这一篇足以

Original: https://blog.csdn.net/qq_54796785/article/details/127703735
Author: 小尘要自信
Title: 【Spring】核心部分之AOP:通过列举代码例子,从底层刨析,深入源码,轻轻松松理解Spring的核心AOP,AOP有这一篇足以

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/795648/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球