Spring_7_1 AOP 使用 XML 实现

2017-09-21  本文已影响22人  mm_cuckoo

@AspectJ

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);
    }
}

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

在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
方法之后>>>增强方法返回数据:增强方法返回的数据
后置增强......

上一篇下一篇

猜你喜欢

热点阅读