理解Spring AOP

简介: 讲述Spring AOP概念的文章,书籍已经不少了,我这里主要说下自己对Spring AOP的理解与它的用法。AOP,即面向切面编程,在编程时我们容易遇到这样的场景,写了某个方法,但是想对这个方法做一些额外的操作,比如方法执行之前做个标记,记录那个对象执行的方法,方法执行完之后进行一些后续处理等等。

讲述Spring AOP概念的文章,书籍已经不少了,我这里主要说下自己对Spring AOP的理解与它的用法。

AOP,即面向切面编程,在编程时我们容易遇到这样的场景,写了某个方法,但是想对这个方法做一些额外的操作,比如方法执行之前做个标记,记录那个对象执行的方法,方法执行完之后进行一些后续处理等等。我们可以使用代理来处理,也可以使用Spring AOP来做这些事。

就像在生活中我们在做某些事情的时候,做事情之前有人干扰,做的时候有人干扰,做不好还有人干扰,做之后还有人干扰...就像一些父母对待子女,子女的一些事情,父母从头到尾全程参与干扰。

概念

切点:我们需要执行的方法,也就是我们需要做的主要的事
切面:在切点周围的各种动作,执行方法之前,之后,等要做的事

编程

通过编程来实现具体的过程。我们单独的使用Spring AOP,虽然它很强大,可以和很多东西结合起来使用。

创建Spring项目

创建一个项目,引入Spring包,引入aspectjweaver包。

1、创建两个类,学生类与父母类,学生类也可以说是子女

public class Student {

    public void findJob(){
        System.out.println("I am find job");
    }

    public void findGrilFirend(){
        System.out.println("I am find girlfriend");
    }

}
package me.aihe;

/**
 * Created by aihe on 2017/7/12.
 */
public class Parent {

    public void before(){
        System.out.println("before,在你做之前,我先替你做点什么");
    }

    public void afterreturn(){
        System.out.println("after return,在你刚做之后,我替你做点什么");
    }

    public void after(){
        System.out.println("after, 最后,再替你做点什么");
    }

    public void throwed(){
        System.out.println("afterthrowing,当你做失败的时候,我也帮你做点什么");
    }
}

2、创建spring配置文件

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

    <bean id="parent" class="me.aihe.Parent" />
    <bean id="student" class="me.aihe.Student" />
    <aop:config>
        <aop:aspect ref="parent">
            <aop:pointcut id="doanything" expression="execution(* me.aihe.Student.*(..))" />
            <aop:before method="before" pointcut-ref="doanything" />
            <aop:after-returning method="afterreturn" pointcut-ref="doanything" />
            <aop:after method="after" pointcut-ref="doanything" />
            <aop:after-throwing method="throwed" pointcut-ref="doanything" />
        </aop:aspect>
    </aop:config>

</beans>

3、创建测试文件

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");
        Student student = (Student) ctx.getBean("student");
        student.findGrilFirend();
    }
}

4、查看运行结果

img_fd8dae51e7ab53bde08842b72a265324.png
运行结果

扩展

Spring AOP的功能比这些要强得多,还有一些知识点请查阅相关资料。

  • around的用法,案例如下
    public Object around(ProceedingJoinPoint joinPoint){
        try {
            System.out.println("before,在你做之前,我先替你做点什么");
            joinPoint.proceed();
            System.out.println("after return,在你刚做之后,我替你做点什么");
        } catch (Throwable throwable) {
//            throwable.printStackTrace();
            System.out.println("afterthrowing,当你做失败的时候,我也帮你做点什么");
        }finally {
            System.out.println("after, 最后,再替你做点什么");
        }
        return null;
    }

然后在Spring配置文件中

    <bean id="parent" class="me.aihe.Parent" />
    <bean id="student" class="me.aihe.Student" />
    <aop:config>
        <aop:aspect ref="parent">
            <aop:pointcut id="doanything" expression="execution(* me.aihe.Student.*(..))" />
            <!--<aop:before method="before" pointcut-ref="doanything" />-->
            <!--<aop:after-returning method="afterreturn" pointcut-ref="doanything" />-->
            <!--<aop:after method="after" pointcut-ref="doanything" />-->
            <!--<aop:after-throwing method="throwed" pointcut-ref="doanything" />-->
            <aop:around method="around" arg-names="joinPoint" pointcut-ref="doanything"/>
        </aop:aspect>
    </aop:config>
  • 注解配置Spring AOP,@Aspect,@Before,@After等等
  • Spring AOP在进行切面的时候传入相关参数,注解传参方法,使用&&,||,!,xml配置文件使用and,or,not

参考

Spring AOP核心概念

总结

这篇文章简单的介绍了Spring AOP的基本概念,基本上是个人理解,可自己看官方解释。然后演示了一个Spring AOP的小案例,

目录
相关文章
|
1月前
|
监控 Java 开发者
Spring AOP动态代理
Spring AOP动态代理
43 1
|
1月前
|
Java Spring 容器
Spring的AOP失效场景详解
Spring的AOP失效场景详解
108 0
|
2月前
|
XML Java 编译器
Spring AOP初步理解及使用
Spring AOP初步理解及使用
49 0
|
2月前
|
Java Spring
[Spring]aop的配置与使用
[Spring]aop的配置与使用
40 0
[Spring]aop的配置与使用
|
29天前
|
设计模式 Java Maven
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
35 1
|
2月前
|
XML Java 数据格式
5个点轻松搞定Spring AOP底层实现原理
AOP 也是 Spring 中一个较为重要的内容,相对于传统的 OOP 模式,AOP 有很多让人难以理解的地方,本篇文章将向大家介绍 AOP 的实现方法及其底层实现,内容包括:
46 1
|
22天前
|
XML Java Maven
Spring之Aop的注解使用
Spring之Aop的注解使用
|
28天前
|
Java Spring
Spring 如何实现 AOP
Spring 如何实现 AOP
17 0
|
1月前
|
Java 编译器 程序员
Spring AOP 和 AspectJ 的比较
Spring AOP 和 AspectJ 的比较
37 0
|
1月前
|
Java Spring
【spring(三)】AOP总结
【spring(三)】AOP总结