【Spring實戰】—— 10 AOP針對參數的通知


通過前面的學習,可以了解到 Spring的AOP可以很方便的監控到方法級別的執行 ,針對於某個方法實現通知響應。

那么對於方法的參數如何呢?

  比如我們有一個方法,每次傳入了一個字符串,我想要知道每次傳入的這個字符串是神馬?這又如何辦到呢!

  舉個Action上面的例子,一個思考者(thinker),每次在思考時,都會傳入一個字符串作為思考的內容。

  我們想要每次獲取到這個思考的內容,實現一個通知。因此讀心者可以通過AOP直接監控到每次傳入的內容。

源碼參考

  首先看一下思考者的接口和實現類:

package com.spring.test.aopmind;

public interface Thinker {
    void thinkOfSomething(String thoughts);
}
package com.spring.test.aopmind;

public class Volunteer implements Thinker{
    private String thoughts;
    public void thinkOfSomething(String thoughts) {
        this.thoughts = thoughts;
    }
    public String getThoughts(){
        return thoughts;
    }
}

  下面是讀心者的接口和實現類:

package com.spring.test.aopmind;

public interface MindReader {
    void interceptThoughts(String thougths);
    String getThoughts();
}
package com.spring.test.aopmind;

public class Magician implements MindReader{
    private String thoughts;
    public void interceptThoughts(String thougths) {
        System.out.println("Intercepting volunteer's thoughts");
        this.thoughts = thougths;
    }
    public String getThoughts() {
        return thoughts;
    }
}

  接着配置好bean.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-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
    
    <bean id="magician" class="com.spring.test.aopmind.Magician"/>
    <bean id="xingoo" class="com.spring.test.aopmind.Volunteer" />
    
    <aop:config proxy-target-class="true">    
        <aop:aspect ref="magician">
            <aop:pointcut id="thinking" 
            expression="execution(* com.spring.test.aopmind.Thinker.thinkOfSomething(String)) and args(thoughts)"/>
            <aop:before pointcut-ref="thinking" method="interceptThoughts" arg-names="thoughts"/>
        </aop:aspect>
    </aop:config>
</beans>

  測試類如下

package com.spring.test.aopmind;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Thinker thinker = (Thinker)ctx.getBean("xingoo");
        thinker.thinkOfSomething("吃點啥呢!");
    }
}

  執行結果:

Intercepting volunteer's thoughts

 

講解說明

  在配置文件中:

  在<aop:before>中指明了要傳入的參數thoughts

  在<aop:pointcut>切點中通過AspectJ表達式鎖定到特定的方法和參數thoughts

  這樣,當執行到方法thinkOfSomething()之前,就會觸發aop,得到參數thoughts,並傳遞給通知類的攔截方法中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM