美文网首页
Spring_7_1 AOP 使用 XML 实现

Spring_7_1 AOP 使用 XML 实现

作者: mm_cuckoo | 来源:发表于2017-09-21 23:34 被阅读22次

    @AspectJ

    • AspectJ 是一个面向切面的框架,它扩展了Java 语言。AspectJ 定义了AOP 语法所以他有一个专门的编译器用来生成遵守编码规范的class 文件。
    • AspectJ 是一个基于java语言的AOP框架。
    • Spring2.0 以后新增了对AspectJ切点表达是支持。
    • @AspectJ 是 AspectJ 新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面。
    • 新版本 Spring 框架,建议使用 AspectJ方式开发 AOP
    • 使用AspectJ 需要导入Spring AOP 集合AspectJ 相关jar包。

    Spring AOP

    • 在Spring里面进行AOP 操作,使用AspectJ 实现。
    • AspectJ 不是 Spring 一部分,和spring 一起使用进行 AOP 操作

    导入jar 包

    包名
    commons-logging-1.1.3.jar
    log4j-1.2.17.jar
    spring-beans-4.2.4.RELEASE.jar
    spring-context-4.2.4.RELEASE.jar
    spring-core-4.2.4.RELEASE.jar
    spring-expression-4.2.4.RELEASE.jar
    spring-aop-4.2.4.RELEASE.jar
    spring-expression-4.2.4.RELEASE.jar
    aopalliance-1.0.jar
    aspectjweaver-1.8.9.jar
    spring-aspects-4.2.4.RELEASE
    包名 下载地址
    aopalliance-1.0.jar http://mvnrepository.com/artifact/aopalliance/aopalliance
    aspectjweaver-1.8.9.jar http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

    在上面得jar 包中aopalliance-1.0.jaraspectjweaver-1.8.9.jar 两个jar包spring的lib 包中是不提供的,需要我们自己下载:

    包名 下载地址
    aopalliance-1.0.jar http://mvnrepository.com/artifact/aopalliance/aopalliance
    aspectjweaver-1.8.9.jar http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

    AOP 实现

    配置约束

    在 spring.xml 文件中添加http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd约束。

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

    创建被增强类

    如下示例中我们为 add() 方法进行增强

    package com.sfox.spring.aop;
    
    public class Book {
        
        public String add(String data1,String data2){
            System.out.println("增强方法 >>>>>data1:" + data1 + ";data2:" + data2);
            return "增强方法返回的数据";
        }
    }
    
    

    创建增强类

    package com.sfox.spring.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class MyBook {
        
        public void before(){
            System.out.println("前置增强......");
        }
        
        public void after(){
            System.out.println("后置增强......");
        }
        
        public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
            
            System.out.println("方法之前.....");
    
            String str = (String) proceedingJoinPoint.proceed(new String[]{"环绕参数1","环绕参数2"});
            
            System.out.println("方法之后>>>增强方法返回数据:" + str);
        }
    }
    
    

    通过上面代码中我们只给出了,前置、后置和环绕,前置增强和后置增强都好理解,这里就不在介绍了,下面介绍一下环绕。

    • 环绕
      • 在使用环绕的时候,方法中传入的 roceedingJoinPoint 对象是必须的,因为我们要通过 proceedingJoinPoint.proceed()方法调用被增强的方法。
    • proceed() 方法说明
      proceed() 方法有两个,一个有参,一个无参
    • 无参:被增强的方法在调用时传的参数不变
    • 有参:被增强的方法在调用时传入的参数被 proceed() 方法中传入的参数替换。

    在spring.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="com.sfox.spring.aop.Book"/>
            <bean id="myBook" class="com.sfox.spring.aop.MyBook"/>
            
            <!-- 2 配置 AOP  -->
            <aop:config>
                <!-- 2.1 配置切入点 
                    expression : 表达式,是设置aop被增强的方法
                 -->
                <aop:pointcut expression="execution(* com.sfox.spring.aop.Book.add(..))" id="addpointcut"/>
                <!-- 2.2配置切面
                    把增强用到方法上
                 -->
                 <aop:aspect ref="myBook">
                    <!-- 配置增强类型、
                        method : 在增强类中使用用到要增强方法上的方法
                        pointcut-ref: 指定要被增强的方法   pointcut-ref 的 value 和 aop:pointcut 中 id 相对应
                      -->
                    <!-- 前置增强 -->
                    <aop:before method="before" pointcut-ref="addpointcut"/>
                    <!-- 后置增强 -->
                    <aop:after method="after" pointcut-ref="addpointcut"/>
                    <!-- 环绕通知 -->
                    <aop:around method="around" pointcut-ref="addpointcut"/>
                 </aop:aspect>
            </aop:config>
    </beans>
    

    测试代码

    public class AopTest {
        @Test
        public void testAop(){
            ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            Book book = (Book) context.getBean("book");
            book.add("1","2");
        }
    }
    

    运行结果

    • 无参
    前置增强......
    方法之前.....
    增强方法 >>>>>data1:传入参数1;data2:传入参数2
    方法之后>>>增强方法返回数据:增强方法返回的数据
    后置增强......
    
    • 有参
    前置增强......
    方法之前.....
    增强方法 >>>>>data1:环绕参数1;data2:环绕参数2
    方法之后>>>增强方法返回数据:增强方法返回的数据
    后置增强......
    
    

    相关文章

      网友评论

          本文标题:Spring_7_1 AOP 使用 XML 实现

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