1. 程式人生 > >Spring 4.0 學習日記(9) ---XML配置實現AOP切面

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