理解Spring AOP

作者: Real_man | 来源:发表于2017-07-12 17:13 被阅读51次

    讲述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 AOP

        本文链接:https://www.haomeiwen.com/subject/kwbbhxtx.html