深入理解Sping——AOP的试水

本文通过定义接口和实现类,结合Spring AOP配置,演示了如何使用AspectJ进行前置和后置增强,展示了如何通过XML配置文件指定切入点和通知,以及不同切面的执行顺序。

深入理解Sping——AOP的试水

定义接口
public interface HelloWorld {
    void printHelloWorld();

    void doPrint();
}
定义接口的实现
public class HelloWorldImpl1 implements HelloWorld {

    public void printHelloWorld() {
        System.out.println("Enter HelloWorldImpl1.printHelloWorld()");
    }

    public void doPrint() {
        System.out.println("Enter HelloWorldImpl1.doPrint()");
        return;
    }
}

public class HelloWorldImpl2 implements HelloWorld {

    public void printHelloWorld() {
        System.out.println("Enter HelloWorldImpl2.printHelloWorld()");
    }

    public void doPrint() {
        System.out.println("Enter HelloWorldImpl2.doPrint()");
        return;
    }
}
定义Handler
public class LogHandler {

    public void LogBefore() {
        System.out.println("Log before method");
    }

    public void LogAfter() {
        System.out.println("Log after method");
    }
}

public class TimeHandler {

    public void printTime() {
        System.out.println("CurrentTime = " + System.currentTimeMillis());
    }
}
定义XML配置
//spring-context-aop.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-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <bean id="helloWorldImpl1" class="com.melon.spring.aop.helloworld.HelloWorldImpl1"/>
    <bean id="helloWorldImpl2" class="com.melon.spring.aop.helloworld.HelloWorldImpl2"/>
    <bean id="timeHandler" class="com.melon.spring.aop.helloworld.TimeHandler"/>
    <bean id="logHandler" class="com.melon.spring.aop.helloworld.LogHandler"/>

    <!--proxy-target-class 为true,基于类代理;为false,基于接口代理-->
    <aop:config>
        <!--
        (1)aspect里面有一个order属性,order属性的数字就是横切关注点的顺序
        (2)把logHandler定义在timeHandler前面,Spring默认以aspect的定义顺序作为织入顺序
        -->
        <aop:aspect id="time" ref="timeHandler">
            <aop:pointcut id="addAllMethod" expression="execution(* com.melon.spring.aop.helloworld.HelloWorld.*(..))"/>
            <aop:before method="printTime" pointcut-ref="addAllMethod"/>
            <aop:after method="printTime" pointcut-ref="addAllMethod"/>
        </aop:aspect>
        <aop:aspect id="log" ref="logHandler" order="2">
            <aop:pointcut id="printLog" expression="execution(* com.melon.spring.aop.helloworld.HelloWorld.*(..))"/>
            <aop:before method="LogBefore" pointcut-ref="printLog"/>
            <aop:after method="LogAfter" pointcut-ref="printLog"/>
        </aop:aspect>
    </aop:config>
</beans>
定义测试类
@Log4j2
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-context-test.xml"})
@WebAppConfiguration
public class AopTest {
    @Test
    public void testHelloWorld() {

        HelloWorld hw1 = (HelloWorld) SpringContextHolder.getBean("helloWorldImpl1");
        HelloWorld hw2 = (HelloWorld) SpringContextHolder.getBean("helloWorldImpl2");
        hw1.printHelloWorld();
        System.out.println();
        hw1.doPrint();

        System.out.println();
        hw2.printHelloWorld();
        System.out.println();
        hw2.doPrint();
    }
}

执行结果:

Log before method
CurrentTime = 1485690199071
Enter HelloWorldImpl1.printHelloWorld()
CurrentTime = 1485690199079
Log after method

Log before method
CurrentTime = 1485690199079
Enter HelloWorldImpl1.doPrint()
CurrentTime = 1485690199079
Log after method

Log before method
CurrentTime = 1485690199079
Enter HelloWorldImpl2.printHelloWorld()
CurrentTime = 1485690199085
Log after method

Log before method
CurrentTime = 1485690199085
Enter HelloWorldImpl2.doPrint()
CurrentTime = 1485690199085
Log after method

转载于:https://my.oschina.net/xjt2014/blog/830729

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值