理解Spring 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、查看运行结果

运行结果

扩展

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的小案例,

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • 什么是AOP AOP(Aspect-Oriented Programming,面向切面编程),可以说是OOP(Ob...
    FX_SKY阅读 20,377评论 1 32
  • 在理解Spring AOP以及理清它与Aspect和cglib之间关系之前,有很多基础工作要做,比如,先对代理模式...
    maxwellyue阅读 1,287评论 0 5
  • 写在前面:本文面向的读者为熟悉Java面向对象程序开发、有过Spring开源框架使用经验的群体。 AOP通常被称为...
    EakonZhao阅读 3,379评论 0 34
  • 遇见,时光故事 阿木篇 你所追寻的答案一点点拼凑,你所有的自以为是随之粉碎。《致十月沉沦》 十月的最初,那天晚上,...
    Haii阅读 177评论 0 0