zoukankan      html  css  js  c++  java
  • 使用aspectJ实现Spring AOP的两种方式

     

    方式一:基于aspectJ的XML配置

    方式二:基于aspectJ的注解方式

    基于aspectJ的XML配置

    1)       引入相关jar

    2)       创建Spring核心配置文件,必须导入aop的约束

    <?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">

    </beans>

    3)       使用表达式配置切入点

    [1] 切入点:实际增强的方法

    [2] execution(<访问修饰符>?<返回值类型><方法名全路径>(<从参数>)<异常>)

    表达式的几种写法:

    ①execution (* cn.aop.Book.add(..))对cn.aop包下的Book类中的所有方法名为add的方法进行增强

     *:表示可以是任意访问修饰符

    cn.aop.Book.add:方法的全路径

    (..):表示可以有参数,也可以没有

                       ②execution (*  cn.aop.Book.*(..))对cn.aop包下的Book类中的所有方法进行增强

                       ③execution(* *.*(..))对所有包下的所有类中的所有方法进行增强

                       ④execution(* save*(..))对所有以save开头的方法进行增强

    前置增强实例:

    <?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">
        <!--1 配置对象(创建对象)-->
       
    <bean id="book" class="cn.bdqn.SpringAspectJ.Book"/>
        <bean id="mybook" class="cn.bdqn.SpringAspectJ.Mybook"/>
        <!--2 配置aop操作-->
       
    <aop:config>
            <!--2.1配置切入点-->
           
    <aop:pointcut expression="execution(* cn.SpringAspectJ.Book.*(..))" id="pointcut1"/>
            <!--2.2配置切面
            把增强用到方法上面
    ,要指定哪个增强-->
           
    <aop:aspect ref="mybook">
                <!--配置增强类型
                method:
    增强类里面使用哪个方法作为前置增强-->
               
    <aop:before method="before" pointcut-ref="pointcut1"/>
            </aop:aspect>
        </aop:config>

    </beans>

    4)       编写测试类

    public class Test {
        @org.junit.Test
        public void test(){
         Book book= (Book) new ClassPathXmlApplicationContext("beans.xml").getBean("book");
         book.add();
        }
    }

    测试结果如下:

    后置增强实例和前置一样

    环绕增强:

    需要增强的类

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

    增强类中的方法::

    public void around(ProceedingJoinPoint proceedingJoinPoint){
        //方法之前
       
    System.out.println("方法之前...");
        //执行被增强的方法
       
    try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        //方法之后
       
    System.out.println("方法之后");
    }

    XML配置文件

    <?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">
        <!--1 配置对象-->
       
    <bean id="book" class="cn.bdqn.SpringAspectJ.Book"/>
        <bean id="mybook" class="cn.bdqn.SpringAspectJ.Mybook"/>
        <!--2 配置aop操作-->
       
    <aop:config>
            <!--2.1配置切入点-->
           
    <aop:pointcut expression="execution(* cn.bdqn.SpringAspectJ.Book.*(..))" id="pointcut1"/>
            <!--2.2配置切面
            把增强用到方法上面
    ,要指定哪个增强-->
           
    <aop:aspect ref="mybook">
                <!--配置增强类型
                method:
    增强类里面使用哪个方法作为前置增强-->

               
    <aop:around method="around" pointcut-ref="pointcut1"/>
            </aop:aspect>
        </aop:config>
    </beans>

    测试类

    public class Test {
        @org.junit.Test
        public void test(){
         Book book= (Book) new ClassPathXmlApplicationContext("beans.xml").getBean("book");
         book.add();
        }
    }

    执行效果

     

    方式二: 基于aspectJ的注解方式

    1)       引入相关jar

    2)       创建Spring核心配置文件,必须导入aop的约束

    以上两步骤直接参照基于aspectJ的XML配置方式

    3)       在Spring配置文件中开启aop操作

    4)       在增强类上面加上@Aspectj的注解

    在增强的方法上面加上@before注解 @before是前置增强

    5)       创建test类,运行效果如下

  • 相关阅读:
    POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)
    UVaLive 5031 Graph and Queries (Treap)
    Uva 11996 Jewel Magic (Splay)
    HYSBZ
    POJ 3580 SuperMemo (Splay 区间更新、翻转、循环右移,插入,删除,查询)
    HDU 1890 Robotic Sort (Splay 区间翻转)
    【转】ACM中java的使用
    HDU 4267 A Simple Problem with Integers (树状数组)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4417 Super Mario (树状数组/线段树)
  • 原文地址:https://www.cnblogs.com/zqr99/p/7682595.html
Copyright © 2011-2022 走看看