Spring的艺术(五):通俗易懂的AOP切面详解

听说微信搜索《Java鱼仔》会变更强哦!

本文收录于JavaStarter ,里面有我完整的Java系列文章,学习或面试都可以看看哦

(一)概述

在前面的学习中,我们已经把Spring的一个核心IOC学习完毕,下面开始学习Spring的另外一个核心–Spring AOP。AOP翻译为面向切面编程,刚开始接触的小伙伴肯定不明白什么是面向切面。简单来讲,面向切面就是对业务逻辑的各个部分进行隔离。

最常见的就是日志与业务逻辑分离,我们就可以通过AOP在业务逻辑执行前写日志,也可以在业务逻辑执行后写日志,而不会动已经写好的业务逻辑代码。

(二)AOP的一些概念

AOP中有以下几个概念:

  • 切入点(Pointcut
    在哪些类,哪些方法上切入(where)

  • 通知(Advice
    在方法执行的什么实际(when:方法前/方法后/方法前后)做什么(what:增强的功能)

  • 切面(Aspect
    切面 = 切入点 + 通知,通俗点就是:在什么时机,什么地方,做什么增强!

  • 织入(Weaving
    把切面加入到对象,并创建出代理对象的过程。(由 Spring 来完成)
    通过设定切入点、通知、切面从而实现AOP想要实现的内容。

AOP定义了五种通知类型:前置通知、后置通知、返回通知、异常通知、环绕通知。

分别代表通知执行的时间点,比如前置通知在业务代码执行前执行。

以上的概念知道就行,接下来会通过代码加深印象。

(三)使用Spring实现AOP

使用AOP时,需要导入一个依赖包,这里把Spring也必须的包同样放进去

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.9.RELEASE</version>
</dependency>

为了模拟业务场景,我们写一个接口和实现类,模拟业务逻辑:

public interface Service {
    public void add();
    public void select();
    public void update();
    public void delete();
}

模拟业务逻辑的实现类

public class ServiceImpl implements Service {
    public void add() {
        System.out.println("add");
    }
    public void select() {
        System.out.println("select");
    }
    public void update() {
        System.out.println("update");
    }
    public void delete() {
        System.out.println("delete");
    }
}

同时在bean.xml中将bean注册到Spring容器里,这个bean.xml中我引入了aop所需要的相关依赖。

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--注册bean-->
    <bean id="service" class="com.javayz.service.ServiceImpl"/>
</beans>

3.1 通过Spring的API实现AOP

实现AOP有两种方式,第一种通过Spring的API,我们新建一个包叫log,在里面新建一个BeforeLog:

public class BeforeLog implements MethodBeforeAdvice {
    /**
     * @param method 执行的目标对象的方法
     * @param objects 参数
     * @param o 目标对象
     * @throws Throwable
     */
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"这个类的"+method.getName()+"这个方法被执行了");
    }
}

这个类继承了MethodBeforeAdvice ,表示这是一个前置通知,继承后需要实现before方法,里面有三个参数:

Method 执行的目标对象的方法

Object参数

Object 目标对象

有没有觉得很眼熟?没错,这简直就是动态代理啊。我们在这里输出一条数据。

接着在bean.xml中将bean注册到Spring容器中,同时配置aop

<!--注册bean-->
<bean id="service" class="com.javayz.service.ServiceImpl"/>
<bean id="beforeLog" class="com.javayz.log.BeforeLog"/>

<!--配置aop-->
<aop:config>
    <!--切入点:要执行的为止,这里需要用execution表达式-->
    <aop:pointcut id="pointcut" expression="execution(* com.javayz.service.ServiceImpl.*(..))"/>
    <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
</aop:config>

配置aop分两步,第一步配置配置切入点,即要执行的位置,这里用的是execution表达式:

execution(* com.javayz.service.ServiceImpl.*(…))

第一个星号表示返回类型,*表示所有的类型

接着是需要拦截的包名下的某个类的某个方法,*表示所有方法,最后的括号表示方法的参数,两个点代表任何参数。

第二步配置通知,这里配置了before通知。

写一个测试方法:

@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Service service = (Service) context.getBean("service");
    service.add();
}

执行后观察结果:

在这里插入图片描述

在业务逻辑代码前执行了我们的切入方法,AOP在没有动业务代码的情况下实现了其他模块代码的切入。

3.2 自定义切面实现AOP

上面这种方式虽然直观,但是过于复杂了,我们可以自己定义个切面

public class MyAspect {
    public void before(){
        System.out.println("业务执行前执行");
    }
    public void after(){
        System.out.println("业务执行后执行");
    }
}

接着配置bean.xml

<bean id="myAspect" class="com.javayz.aspect.MyAspect"/>
<aop:config>
    <aop:aspect ref="myAspect">
        <aop:pointcut id="pointcut" expression="execution(* com.javayz.service.ServiceImpl.*(..))"/>
        <aop:before method="before" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

首先把自己定义的aspect注册,然后引入aop,设置切入点,如果使用idea的话aop会给出五种切入方式,这里选择before。

在这里插入图片描述
依旧执行上面的测试代码,观察结果:

在这里插入图片描述

(四)使用注解的方式实现AOP

使用注解的方式实现AOP更加简单,首先定义一个切面类:

//标注这个类是一个切面
@Aspect
public class AnnotationAspect {

    @Before("execution(* com.javayz.service.ServiceImpl.*(..))")
    public void before(){
        System.out.println("方法执行前执行");
    }
}

通过@Aspect标明这是一个切面,通过@Before、@After、@Around、@AfterReturning、@AfterThrowing对应五种注解。

接着在配置文件中配置开启注解:

<!--注入bean-->
<bean id="annotationAspect" class="com.javayz.annotation.AnnotationAspect"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java鱼仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值