Spring-AOP @AspectJ切點函數(shù)之@annotation()用法
@annotation()概述
@annotation表示標(biāo)注了某個注解的所有方法。
下面通過一個實例說明@annotation()的用法。 AnnotationTestAspect定義了一個后置切面增強,該增強將應(yīng)用到標(biāo)注了NeedTest的目標(biāo)方法中。
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster

首先我們先自定義一個注解@NeedTest。
如何自定義注解請參考Java-Java5.0注解解讀
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)
// 聲明可以使用該注解的目標(biāo)類型
@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);
}
接口實現(xiàn)類 兩個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()方法標(biāo)注了@NeedTest, NaughtWaiter#serverTo()也標(biāo)注了@NeedTest,我們的目標(biāo)就是將后置增強織入到這兩個標(biāo)注了@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表示標(biāo)注了某個注解的所有方法
*
* @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自動應(yīng)用切面,配置文件如下
<?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命名空間進(jìn)行配置 --> <!-- 基于@AspectJ切面的驅(qū)動器 --> <aop:aspectj-autoproxy/> <!-- 目標(biāo)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");
// 必須是接口類型,否則拋類型轉(zhuǎn)換異常
Waiter waiter = (Waiter) ctx.getBean("naiveWaiter");
// 因為greetTo標(biāo)注了@NeedTest,因此會被后置增強
waiter.greetTo("XiaoGongJiang");
waiter.serverTo("XiaoGongJiang");
Waiter naughtWaiter = (Waiter) ctx.getBean("naughtWaiter");
// serverTo標(biāo)注了@NeedTest,因此會被后置增強
naughtWaiter.serverTo("XiaoGongJiang");
}
}
運行結(jié)果:
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
從輸出結(jié)果中可以看出,切面被正確的織入到了標(biāo)注有@NeedTest注解的方法中。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java的項目構(gòu)建工具M(jìn)aven的配置和使用教程
Maven是Java世界中的項目管理和構(gòu)建自動化工具,基于POM項目對象模型的思想,下面我們就具體來看一下具體的Java的項目構(gòu)建工具M(jìn)aven的配置和使用教程:2016-05-05
windows 32位eclipse遠(yuǎn)程hadoop開發(fā)環(huán)境搭建
這篇文章主要介紹了windows 32位eclipse遠(yuǎn)程hadoop開發(fā)環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下2016-07-07
淺談spring方法級參數(shù)校驗(@Validated)
這篇文章主要介紹了淺談spring方法級參數(shù)校驗(@Validated),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
java設(shè)計模式之實現(xiàn)對象池模式示例分享
對象池模式經(jīng)常用在頻繁創(chuàng)建、銷毀對象(并且對象創(chuàng)建、銷毀開銷很大)的場景,比如數(shù)據(jù)庫連接池、線程池、任務(wù)隊列池等。本代碼簡單,沒有限制對象池大小2014-02-02
springmvc使用JSR-303進(jìn)行數(shù)據(jù)校驗實例
本篇文章主要介紹了詳解springmvc使用JSR-303進(jìn)行數(shù)據(jù)校驗,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02

