【轉】spring 自定義注解(annotation)與 aop獲取注解


首先我們先介紹Java自定義注解。

在開發過程中,我們實現接口的時候,會出現@Override,有時還會提示寫@SuppressWarnings。其實這個就是Java特有的特性,注解。

注解就是某種注解類型的一種實例,我們可以把它用在某個類上進行標注。下面這張圖解釋注解都是什么?

image

上圖可以看出注解大體分為三種:元注解,標記注解,一般注解;

這一塊其他的我就不多做介紹,我們這里主要說一下如何定義自己的注解,在這之前我們必須了解標准元注解和相關定義注解的語法。
元注解的作用就是負責注解其他注解。Java5.0定義了4個標准的meta-annotation類型,它們被用來提供對其它 annotation類型作說明。Java5.0定義的元注解:

1.@Target,
2.@Retention,
3.@Documented,
4.@Inherited

@Target
@Target說明了Annotation所修飾的對象范圍:Annotation可被用於 packages、types(類、接口、枚舉、Annotation類型)、類型成員(方法、構造方法、成員變量、枚舉值)、方法參數和本地變量(如循環變量、catch參數)。在Annotation類型的聲明中使用了target可更加明晰其修飾的目標。
作用:用於描述注解的使用范圍(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
  1. CONSTRUCTOR:用於描述構造器
  2. FIELD:用於描述符
  3. LOCAL_VARIABLE:用於描述局部變量
  4. METHOD:用於描述方法
  5. PACKAGE:用於描述包
  6. PARAMETER: 用於描述參數
  7. TYPE: 用於描述類、接口(包括注解類型)或者enum聲明
@Retention
@Retention定義了該Annotation被保留的時間長短:某些Annotation僅出現在源代碼中,而被編譯器丟棄;而另一些卻被編譯在class文件中;編譯在class文件中的Annotation可能會被虛擬機忽略,而另一些在class被裝載時將被讀取(請注意並不影響class的執行,因為Annotation與class在使用上是被分離的)。使用這個meta-Annotation可以對 Annotation的“生命周期”限制。
作用:表示需要在什么級別保存該注釋信息,用於描述注解的生命周期(即:被描述的注解在什么范圍內有效)
取值(RetentionPoicy)有:
  1. SOURCE:在源文件中有效(即源文件保留)
  2. CLASS:在class文件中有效(即class保留)
  3. RUNTIME:在運行時有效(即運行時保留)
 @Documented
@Documented用於描述其它類型的annotation應該被作為被標注的程序成員的公共API,因此可以被例如javadoc此類的工具文檔化。Documented是一個標記注解,沒有成員。
 @Inherited
@Inherited 元注解是一個標記注解,@Inherited闡述了某個被標注的類型是被繼承的。如果一個使用了@Inherited修飾的annotation類型被用於一個class,則這個annotation將被用於該class的子類。
注意:@Inherited annotation類型是被標注過的class的子類所繼承。類並不從它所實現的接口繼承annotation,方法並不從它所重載的方法繼承annotation。
當@Inherited annotation類型標注的annotation的Retention是RetentionPolicy.RUNTIME,則反射API增強了這種繼承性。如果我們使用java.lang.reflect去查詢一個@Inherited annotation類型的annotation時,反射代碼檢查將展開工作:檢查class和其父類,直到發現指定的annotation類型被發現,或者到達類繼承結構的頂層。

自定義注解

使用@interface自定義注解時,自動繼承了java.lang.annotation.Annotation接口,由編譯程序自動完成其他細節。在定義注解時,不能繼承其他的注解或接口。@interface用來聲明一個注解,其中的每一個方法實際上是聲明了一個配置參數。方法的名稱就是參數的名稱,返回值類型就是參數的類型(返回值類型只能是基本類型、Class、String、enum)。可以通過default來聲明參數的默認值。
定義注解格式:
public @interface 注解名 {定義體}

   注解參數的可支持數據類型:
   1. 所有基本數據類型(int,float,boolean,byte,double,char,long,short)
   2. String類型
   3. Class類型
   4. enum類型
   5. Annotation類型
   6. 以上所有類型的數組
  
  Annotation類型里面的參數該怎么設定:
  第一,只能用public或默認(default)這兩個訪問權修飾.例如,String value();這里把方法設為defaul默認類型;
  第二,參數成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基本數據類型和 String,Enum,Class,annotations等數據類型,以及這一些類型的數組.例如,String value();這里的參數成員就為String;
  第三,如果只有一個參數成員,最好把參數名稱設為"value",后加小括號.例:下面的例子FruitName注解就只有一個參數成員。


使用示例:

CacheRedis.java

import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface CacheRedis { String key(); int expireTime() default 600; }

CacheService.java

import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Aspect @Component public class CacheService { Logger logger = LoggerFactory.getLogger(CacheService.class); @Pointcut(value = "@annotation(com.meizu.bro.service.test.CacheRedis)") public void pointCut(){} @Before(value = "pointCut() && @annotation(cacheRedis)") public void before(CacheRedis cacheRedis) { logger.info("the result of this method will be cached."); } @AfterReturning(value = "pointCut() && @annotation(cacheRedis)",returning = "result") public void after(CacheRedis cacheRedis,Object result) { String key = cacheRedis.key(); int expireTime = cacheRedis.expireTime(); //do something... logger.info("-----redis-----[key = " + key + "]"+"[expireTime = " + expireTime + "]"); logger.info("the result of this method is" + result + ",and has been cached."); } //@Around("pointCut() && @annotation(cacheRedis)") //public Object setCache(ProceedingJoinPoint joinPoint,CacheRedis cacheRedis) { // Object result = 1; // // Method method = getMethod(joinPoint);//自定義注解類 // //CacheRedis cacheRedis = method.getAnnotation(CacheRedis.class);//獲取key值 // String key = cacheRedis.key(); // int expireTime = cacheRedis.expireTime(); // //獲取方法的返回類型,讓緩存可以返回正確的類型 // Class returnType =((MethodSignature)joinPoint.getSignature()).getReturnType(); // // logger.info("[key = " + key + "]"+"[expireTime = " + expireTime + "]"); // // return result; //} // //private Method getMethod(ProceedingJoinPoint joinPoint) { // //獲取參數的類型 // Method method = null; // try { // Signature signature = joinPoint.getSignature(); // MethodSignature msig = null; // if (!(signature instanceof MethodSignature)) { // throw new IllegalArgumentException("該注解只能用於方法"); // } // msig = (MethodSignature) signature; // method = joinPoint.getTarget().getClass().getMethod(msig.getName(), msig.getParameterTypes()); // } catch (NoSuchMethodException e) { // logger.error("annotation no sucheMehtod", e); // } catch (SecurityException e) { // logger.error("annotation SecurityException", e); // } // return method; //} } 

測試接口TestController.java

@Controller public class TestController { @Autowired private TestService testService; @RequestMapping(value = "/test") public ModelAndView myTest() { int test = testService.test(10); return ViewUtil.buildStandardJsonViewByObj(test); } @RequestMapping(value = "/test1") public ModelAndView myTest1() { String yanyi = testService.test1("yanyi"); return ViewUtil.buildStandardJsonViewByObj(yanyi); } }

TestService.java

public interface TestYanyiService { int test(int i); String test1(String i1); }

TestServiceImpl.java

import org.springframework.stereotype.Service; @Service public class TestYanyiServiceImpl implements TestYanyiService { @Override @CacheRedis(key = "test",expireTime = 10) public int test(int i) { return 0; } @Override @CacheRedis(key = "test1") public String test1(String i1) { return i1; } }

代碼完成后,需要在pom文件中導入以下依賴:

            <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency>

這一塊要注意:aspectjweaver包的版本和JDK版本有關聯。如果是JDK 1.7及以上的用戶,需要aspectjweaver包的版本不能過低(JDK1.7 —— aspectJ1.7.3+)。否則會報錯:報錯error at ::0 can't find referenced pointcut。

接下來,在Spring配置文件里進行以下配置

<!-- 啟動對@AspectJ注解的支持 --> <aop:aspectj-autoproxy/> <!--通知spring使用cglib而不是jdk的來生成代理方法 AOP可以攔截到Controller-> <aop:aspectj-autoproxy proxy-target-class="true" /> 

ok! 完成后,調用測試接口,可以看到在控制窗口打印切面類前置和后置的相關log。這時候,我們就可以在里邊實現自己的功能咯。(我這里准備實現redis緩存功能。)


免責聲明!

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



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