Spring 4.0 学习日记(9) ---XML配置实现AOP切面

Spring创建代理的规则

1.默认使用Java动态代理来创建AOP代理
2.当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB

其实Xml配置更简单
直接看代码就懂了

接口类

package com.wow.AopMessageInstance;

public interface HelloWorld {


       void printHelloWorld();

       void doPrint();

       String getReturn();
}

实现类1

package com.wow.AopMessageInstance;

public class HelloWorldImpl implements HelloWorld {

    @Override
    public void printHelloWorld() {
        System.out.println("Enter HelloWorldImpl.printHelloWorld()");

    }

    @Override
    public void doPrint() {
        System.out.println("Enter HelloWorldImpl.doPrint()");
    }

    @Override
    public String getReturn() {
        String str = "HelloWorldImpl" ;
        System.out.println("Enter HelloWorldImpl.getReturn()");
        return str;
    }

}

实现类2

package com.wow.AopMessageInstance;

public class HelloWorldImplAnother implements HelloWorld {

    @Override
    public void printHelloWorld() {
        System.out.println("Enter HelloWorldImplAnother.printHelloWorld()");

    }

    @Override
    public void doPrint() {
        System.out.println("Enter HelloWorldImplAnother.doPrint()");

    }

    @Override
    public String getReturn() {
        String str = "HelloWorldImpl" ;
        System.out.println("Enter HelloWorldImplAnother.getReturn()");
        return str;
    }

}

Aop切面

package com.wow.AopMessageInstance;

public class HelloWorldAop {

     public void printTime()
        {
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }

     public void  getReturn(String obj)
        {
            System.out.println("ReturnValue = " + obj);
        }
}

测试类

package com.wow.AopMessageInstance;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldTest {

    public static void main(String[] args) {

        ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld hw = (HelloWorld) app.getBean("helloWorldImpl");
        HelloWorld hwa = (HelloWorld) app.getBean("helloWorldImplAnother");
        hw.printHelloWorld();
        hw.doPrint();
        hw.getReturn();
        System.out.println("----");
        hwa.printHelloWorld();
        hwa.doPrint();
        hwa.getReturn();

    }

}

beans.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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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 = "helloWorldImpl" class = "com.wow.AopMessageInstance.HelloWorldImpl"></bean>
    <bean id = "helloWorldImplAnother" class = "com.wow.AopMessageInstance.HelloWorldImplAnother"></bean>
    <bean id = "helloWorldAop" class = "com.wow.AopMessageInstance.HelloWorldAop"></bean>       

    <!-- 如果只想织入接口中的某些方法 只用修改expresion的匹配方式就好了-->

    <aop:config>
        <aop:aspect id = "aop" ref = "helloWorldAop">
             <aop:pointcut id="pointCut" expression="execution(* com.wow.AopMessageInstance.*.*(..))" />
             <aop:before method="printTime" pointcut-ref="pointCut" />
             <aop:after method="printTime" pointcut-ref="pointCut" />
             <aop:after-returning method="getReturn" pointcut-ref="pointCut" returning="obj"/>             
        </aop:aspect>

    <!-- 如果需要有多个切面 只要在这里再写一个aop:aspect属性就好了  对于多个切面的前后顺序 可以用到order属性 -->

    <!--    <aop:aspect id="time" ref="timeHandler" order="1">
                <aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.print*(..))" />
                <aop:before method="printTime" pointcut-ref="addTime" />
                <aop:after method="printTime" pointcut-ref="addTime" />
            </aop:aspect>
            <aop:aspect id="log" ref="logHandler" order="2">
                <aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.do*(..))" />
                <aop:before method="LogBefore" pointcut-ref="printLog" />
                <aop:after method="LogAfter" pointcut-ref="printLog" />
            </aop:aspect> -->



    </aop:config>



</beans>  

打印信息

八月 02, 2017 11:32:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@142b7711: startup date [Wed Aug 02 23:32:25 CST 2017]; root of context hierarchy
八月 02, 2017 11:32:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
CurrentTime = 1501687946276
Enter HelloWorldImpl.printHelloWorld()
CurrentTime = 1501687946276
CurrentTime = 1501687946277
Enter HelloWorldImpl.doPrint()
CurrentTime = 1501687946277
CurrentTime = 1501687946277
Enter HelloWorldImpl.getReturn()
CurrentTime = 1501687946277
ReturnValue = HelloWorldImpl
----
CurrentTime = 1501687946277
Enter HelloWorldImplAnother.printHelloWorld()
CurrentTime = 1501687946277
CurrentTime = 1501687946278
Enter HelloWorldImplAnother.doPrint()
CurrentTime = 1501687946278
CurrentTime = 1501687946278
CurrentTime = 1501687946278
ReturnValue = HelloWorldImplAnother
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值