スプリング[戦闘] - 通知の10個のAOPパラメータ

以前の研究では、我々は理解することができます  春AOPを簡単にメソッドの実装のレベルを監視することができ 、通知に応じて実施される方法を。

それでは、どの方法それのためのパラメータ?

  例えば、我々はこの方法を持って、文字列を渡すたびに、私は、文字列は、この神の馬に渡されるたびに、知りたいですか?これはそれを行う方法です!

  アクションは、上記の例を与え、人リフレクション(考える人)は、各時間の検討は、リフレクションの内容として文字列を渡します。

  たびに、私たちは、事前の通知を達成するために、この考え方に取得したいです。だから、直接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>

  Testクラス次のように

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前>に

  で<AOP:ポイントカット>接点式のAspectJによって思考をロックし、パラメータの特定の方法に

  このように、()をthinkOfSomethingときに方法を実行する前に、それは、AOPをトリガするパラメータの考えを得るために、クラスを通知するためのインターセプトメソッドに渡されます。

ます。https://my.oschina.net/u/204616/blog/545223で再現

おすすめ

転載: blog.csdn.net/weixin_34343689/article/details/91989363