Spring框架 -- AOP编程

一.Aop概述

Aspect Object Programming 面向切面编程,采取横向抽取机制,取代的传统的纵向继承体系重复性代码

连接点(Join point)
  • 类里面哪些方法可以被增强,这些方法称为连接点|
切入点(Pointcut)
  • 在类中可以被增强的方法有很多,但是当你实际操作时实现了方法增强的方法称为切入点
通知/增强(Advice)
  • 增强扩展功能的逻辑,称谓增强。例如扩展日志功能,这个日志功能称谓增强。

    • 前置通知:在一个方法执行之前执行。
    • 后置通知:在方法执行之后执行。
    • 返回后通知:在方法执行完成,当返回正确才会执行。
    • 抛出异常后通知:在方法抛出异常后执行
    • 环绕通知:在方法之前和之后执行。
切面(Aspect)
  • 把增强应用到具体方法上,这个过程称谓切。把增强用到切入点过程。

二.@AspectJ的aop操作(配置文件实现)

  • 使用表达式配置切入点

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
1.execution(* com.huan.web.book_aop.Book.add(..))
2.execution(* com.huan.web.book_aop.Book.*(...))
3.execution(* *.*(...))
4.execution(* book*(...))匹配所有book开头的方法

  • 配置文件
<!--AOP-->
    <!--配置对象-->
    <bean id = "book" class="com.huan.web.book_aop.Book"></bean>
    <bean id = "myBook" class="com.huan.web.book_aop.MyBook"></bean>
    <!--配置aop操作-->
    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="pointcut1" expression="execution(* com.huan.web.book_aop.Book.add(..))"/>
        <!--配置切面
        把增强用到方法上面
        -->
        <aop:aspect ref="myBook">
            <!--配置增强类型
            method:增强类里面使用哪个方法作为前置
            -->
            <aop:before method="beforeBook" pointcut-ref="pointcut1"/>
            <!--后置增强-->
            <aop:after method="afterBook" pointcut-ref="pointcut1"/>
            <!--环绕执行-->
            <aop:around method="aroundBook" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
  • Book.java
package com.huan.web.book_aop;

public class Book {
    public void add (){
        System.out.println("add---------");
    }
}
  • myBook.java
package com.huan.web.book_aop;

public class MyBook {
    public void beforeBook(){
        System.out.println("前置增强------------");
    }
}
  • Test.java
package com.huan.web.book_aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {
    public void beforeBook(){
        System.out.println("前置增强------------");
    }
    public void afterBook(){
        System.out.println("后置增强-----------");
    }

//    环绕增强
    public void aroundBook(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        方法前
        System.out.println("环绕前------");
//        执行方法
        proceedingJoinPoint.proceed();
//        方法后
        System.out.println("环绕后------");
    }
}

三.@AspectJ的aop操作(注解实现)

  • 1.配置xml
<!--开启aop扫描-->
    <aop:aspectj-autoproxy/>
    <!--AOP-->
    <!--创建对象-->
    <bean id="book" class="com.huan.web.book_aop.Book"/>
    <bean id="myBook" class="com.huan.web.book_aop.MyBook"/>
  • Book.java
package com.huan.web.book_aop;

public class Book {
    public void add (){
        System.out.println("add---------");
    }
}

MyBook.java

package com.huan.web.book_aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyBook {

    @Before(value = "execution(* com.huan.web.book_aop.Book.*(..))")
    public void beforeBook(){
        System.out.println("前置增强------------");
    }
    @After(value = "execution(* com.huan.web.book_aop.Book.*(..))")
    public void afterBook(){
        System.out.println("后置增强-----------");
    }

//    环绕增强
    @Around(value = "execution(* com.huan.web.book_aop.Book.*(..))")
    public void aroundBook(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        方法前
        System.out.println("环绕前------");
//        执行方法
        proceedingJoinPoint.proceed();
//        方法后
        System.out.println("环绕后------");
    }
}
  • Test.java
package com.huan.web.book_aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

@Controller
public class TestDemo {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-aop.xml");
        Book book = (Book)context.getBean("book");
        book.add();
    }
}
注解实现
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容