panda白话 - Spring AOP 入门+实战

Spring 两大🐮特性:IOC 、AOP
面试必问😄
基本上大家都是对IOC理解比较深刻,Spring通过IOC容器帮我们管理bean嘛
AOP其实使用也很广泛,只是你没咋关注过
因为基本属于框架级的,基本配置一次就不用管了,那起码要看懂呀,
let`s go~~

AOP

定义: Oriented Object Progamming (面向切面编程)

概念:

  • 连接点 - joint point

  • 切入点 - point cut

  • 通知 - advice

  • 织入 - weaving

  • 切面 - aspect
    在这里插入图片描述
    Panda 理解:

  • AOP 的本质就是动态代理

  • AOP本质功能就是增强目标对象的方法

  • 我们可以通过上图,看图理解这几个概念:
    1、首先每个目标对象的想被aop代理的方法都是point cut(切入点)
    2、aspect(切面)就像一面墙挡在前面,Aspect其实就是一个,来管理通知、切入点的
    3、Spring会监听每个切入点方法的调用,当有切入点方法调用时,会动态生成一个AOP Proxy代理对象
    4、想要给切入点增强的方法就是advice(通知),分为5种通知
    5、代理对象会将通知weaving(织入)到切入点,所以织入就是个动作

说白了就是你要执行的方法被我代理了,我要在你这个方法执行的前前后后、左左右右,呜呜宣宣搞点事情

Advice 通知类型

  • before - 前置通知 - 目标方法执行前通知
  • after - 后置通知 - 目标方法执行后通知
  • afterReturning - 目标方法正常返回后通知
  • afterThrowing - 目标方法抛出异常后通知
  • around - 环绕通知

dont talk. show me the code

配置文件方式 实现AOP

  • 先搞一个目标类,两个简单方法 method1、method2
  • 再搞个切面类,一个before方法做前置通知
  • 再搞个配置文件,配置切面类、通知方法、切入点方法,
  • 完事、开测
  • 在这里插入图片描述

1、目标类

public class TargetObj {
    public void method1() throws Exception { System.out.println("method1。。");}
    public void method2(){System.out.println("method2。。"); }
    }

2、切面类

public class PandaAspect {
    public void before(){
        System.out.println("before。。");
    }

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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <bean id = "target" class = "aop2.TargetObj"></bean>
    <bean id = "advice" class = "aop2.PandaAdvice"></bean>

    <aop:config>
        <!-- 切面 -->
        <aop:aspect ref="advice">
            <aop:before method="before" pointcut="execution(* *..*.method1(..))"/>
        </aop:aspect>
    </aop:config>
</beans>

3 、开测

public static void main(String[] args) throws Exception {
		//先读取配置文件,将bean加载到ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean
        TargetObj target = (TargetObj) context.getBean("target");
        //执行目标类方法
        target.method1();
    }

执行结果:
在这里插入图片描述
会了一个通知类型,其他类型举一反三就好了。。


注解方式 实现AOP

  • 先搞一个目标类,两个简单方法 method1、method2
  • 再搞个切面类,一个before方法做前置通知
  • 再搞个配置文件,配置切面类、通知方法、切入点方法,
  • 完事、开测
    在这里插入图片描述

1、目标类还是那个目标类

2、切面类,通过注解配置

package annotation;

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

@Component("advice")
@Aspect
public class PandaAdvice {
    @Before(value = "execution(* *..*.method())")
    public void before(){
        System.out.println("...before...");
    }
    @After(value = "execution(* *..*.method())")
    public void after(){
        System.out.println("...after...");
    }
    @AfterThrowing(value = "execution(* *..*.method())")
    public void afterThrowing(){
        System.out.println("...afterThrowing...");
    }
    @AfterReturning(value = "execution(* *..*.method())")
    public void afterReturning(){
        System.out.println("...AfterReturning...");
    }
    @Around(value = "execution(* *..*.method())")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("...around start...");
        joinPoint.proceed();
        System.out.println("...around end...");

    }
}

3、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns 名称空间 schemaLocation 名称空间对应约束文件-->
<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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd ">
    <!--Spring 自动扫描-->
    <context:component-scan base-package="annotation"></context:component-scan>
    <!--开启aop-->
    <aop:aspectj-autoproxy/>
</beans>

4、开测

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        TargetObj target = (TargetObj)context.getBean("target");
        target.method();
    }

执行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值