1. 程式人生 > >Spring-AOP @AspectJ切點函式之@annotation()

Spring-AOP @AspectJ切點函式之@annotation()

@annotation()概述

@annotation表示標註了某個註解的所有方法。

下面通過一個例項說明@annotation()的用法。 AnnotationTestAspect定義了一個後置切面增強,該增強將應用到標註了NeedTest的目標方法中。

例項

這裡寫圖片描述

首先我們先自定義一個註解@NeedTest。

package com.xgj.aop.spring.advisor.aspectJ.function;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import
java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * * * @ClassName: NeedTest * * @Description: 自定義註解@NeedTest * * @author: Mr.Yang * * @date: 2017年8月26日 下午11:19:12 */ // 宣告註解的保留期限 @Retention(RetentionPolicy.RUNTIME) // 宣告可以使用該註解的目標型別
@Target(ElementType.METHOD) @Documented public @interface NeedTest { // 宣告註解成員 boolean value() default false; }

下面我們定義介面 Waiter

package com.xgj.aop.spring.advisor.aspectJ.function;

public interface Waiter {
    public void greetTo(String clientName);

    public void serverTo(String clientName);
}

介面實現類 兩個NaiveWaiter 和 NaughtWaiter

package com.xgj.aop.spring.advisor.aspectJ.function;

public class NaiveWaiter implements Waiter {

    @NeedTest(true)
    @Override
    public void greetTo(String clientName) {
        System.out.println("NaiveWaiter:greet to " + clientName);
    }

    @Override
    public void serverTo(String clientName) {
        System.out.println("NaiveWaiter:server to " + clientName);
    }

    public void smile(String clientName, int times) {
        System.out.println("NaiveWaiter:smile to  " + clientName + " " + times
                + " times");
    }
}
package com.xgj.aop.spring.advisor.aspectJ.function;

public class NaughtWaiter implements Waiter {

    @Override
    public void greetTo(String clientName) {
        System.out.println("NaughtWaiter:greet to " + clientName);
    }

    @NeedTest(true)
    @Override
    public void serverTo(String clientName) {
        System.out.println("NaughtWaiter:server to " + clientName);
    }

    public void joke(String clientName, int times) {
        System.out.println("NaughtyWaiter:play " + times + " jokes to "
                + clientName);
    }
}

我們可以看到 NaiveWaiter#greetTo()方法標註了@NeedTest, NaughtWaiter#serverTo()也標註了@NeedTest,我們的目標就是將後置增強織入到這兩個標註了@NeedTest的方法中。

接下來編寫切面的橫切邏輯

package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

/**
 * 
 * 
 * @ClassName: AnnotationTestAspect
 * 
 * @Description: 切面 、 後置增強 ,@annotation表示標註了某個註解的所有方法
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年8月26日 下午11:23:53
 */

@Aspect
public class AnnotationTestAspect {

    @AfterReturning("@annotation(com.xgj.aop.spring.advisor.aspectJ.function.NeedTest)")
    public void needTest() {
        System.out.println("needTest() executed,some logic is here");
    }

}

接下來通過Spring自動應用切面,配置檔案如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 使用基於Schema的aop名稱空間進行配置 -->


<!-- 基於@AspectJ切面的驅動器 -->
<aop:aspectj-autoproxy/>

<!-- 目標Bean -->
<bean id="naiveWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaiveWaiter"/>

<bean id="naughtWaiter" class="com.xgj.aop.spring.advisor.aspectJ.function.NaughtWaiter"/>

<!-- 使用了@AspectJ註解的切面類 -->
<bean class="com.xgj.aop.spring.advisor.aspectJ.function.annotationFun.AnnotationTestAspect"/>

</beans>

最後編寫測試程式碼:

package com.xgj.aop.spring.advisor.aspectJ.function.annotationFun;

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

import com.xgj.aop.spring.advisor.aspectJ.function.Waiter;

public class AnnotationTestAspcetTest {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "com/xgj/aop/spring/advisor/aspectJ/function/annotationFun/conf-annotation.xml");

        // 必須是介面型別,否則拋型別轉換異常
        Waiter waiter = (Waiter) ctx.getBean("naiveWaiter");

        // 因為greetTo標註了@NeedTest,因此會被後置增強
        waiter.greetTo("XiaoGongJiang");
        waiter.serverTo("XiaoGongJiang");

        Waiter naughtWaiter = (Waiter) ctx.getBean("naughtWaiter");
        // serverTo標註了@NeedTest,因此會被後置增強
        naughtWaiter.serverTo("XiaoGongJiang");
    }
}

執行結果:

2017-08-27 01:24:22,551  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ac604: startup date [Sun Aug 27 01:24:22 BOT 2017]; root of context hierarchy
2017-08-27 01:24:22,647  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/annotationFun/conf-annotation.xml]
NaiveWaiter:greet to XiaoGongJiang
needTest() executed,some logic is here
NaiveWaiter:server to XiaoGongJiang
NaughtWaiter:server to XiaoGongJiang
needTest() executed,some logic is here

從輸出結果中可以看出,切面被正確的織入到了標註有@NeedTest註解的方法中。