• 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)


    【AOP】操作相关术语

    • Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点。
    • Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add和update方法,实际增强的方法称为切入点
    • Advice(通知/增强):增强的逻辑,称为增强,比如扩展日志功能,这个日志功能称为增强。

        前置通知:在方法之前执行

        后置通知:在方法之后执行

        异常通知:方法出现异常

        最终通知:在后置之后执行

        环绕通知:在方法之前和之后执行

    • Aspect(切面):把增强应用到具体方法上面,这个过程称为切面,把增强用到切入点过程。

    【Spring】的【AOP】操作(基于aspectj的xml方式)

    • spring里面进行aop的操作,使用aspectj实现。本身不是spring的一部分,是单独的一个框架,只是和spring一起做spring的aop的操作。
    • Spring2.0以后增加对aspectj的支持。
    • 新版本的spring框架,建议使用aspectj的方法开发aop。
    • 使用aspectj实现aop的两种方式:

        基于aspectj的xml配置

        基于aspectj的注解方式

      AOP操作准备工作

    • 除了导入基本的jar包之外,还需要导入aop相关的jar包

    • 导入AOP的约束

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

      使用表达式配置切入点:

        1、切入点:实际要增强的办法

        2、表达式:execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

      1.   Execution(* com.aop.MyBook.add(..))
      2.   Execution(* com.aop.MyBook.*(..))
      3.   Execution(* *.*.add(..))
      4.   匹配所有的save开头的方法execution(* save*(..))
    1. 代码演示:

      第一步创建Book和MyBook两个类

    2. 1 public class Book {
      2     public void book(){
      3         System.out.println("book------");
      4     }
      5 }
    3. 1 public class MyBook {
      2     public void before1(){
      3         System.out.println("前置增强----------");
      4     }
      5 }

      第二步:创建配置文件

    4.  1 <?xml version="1.0" encoding="UTF-8"?>
       2 <beans xmlns="http://www.springframework.org/schema/beans"
       3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       4     xmlns:aop="http://www.springframework.org/schema/aop"
       5     xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
       6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       7         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
       9     <!-- 配置对象 -->
      10     <bean id="book" class="com.aop.Book"></bean>
      11     <bean id="mybook" class="com.aop.MyBook"></bean>
      12     <!-- 配置AOP操作 -->
      13     <aop:config>
      14         <!-- 配置切入点 表示对那个方法进行增强 -->
      15         <aop:pointcut expression="execution(* com.aop.Book.*(..))" id="pointcut"/>
      16         <!-- 配置切面 把增强的方法用在哪个方法上面-->
      17         <aop:aspect ref="mybook">
      18             <!-- 配置增强类型 method:增强类里面使用哪个方法作为前置 -->
      19             <aop:before method="before1" pointcut-ref="pointcut"/>
      20         </aop:aspect>
      21     </aop:config>
      22 </beans>

      第三步:创建测试方法

      1. 1 @Test
        2     public void testService(){
        3         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3aop.xml");
        4         Book book = (Book)applicationContext.getBean("book");
        5         book.book();
        6     }

        测试结果

        1.  前置增强----------
        2.  book------
        2017-08-18  19:12:37
  • 相关阅读:
    水晶报表 VS2010 应用
    crystalreport使用方法
    spring MVC核心思想
    一台服务器配置多个TOMCAT
    SQL server索引
    锁机制
    JAVA书籍
    Live 直播过程
    html5 video微信浏览器视频不能自动播放
    设计模式 抽象工厂模式
  • 原文地址:https://www.cnblogs.com/angelye/p/7391409.html
Copyright © 2020-2023  润新知