當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringAOP02 自定义注解
生活随笔
收集整理的這篇文章主要介紹了
SpringAOP02 自定义注解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1 自定義注解
1.1 創建自定義注解
從java5開始就可以利用 @interface 來定義自定義注解
技巧01:注解不能直接干擾程序代碼的運行(即:注解的增加和刪除操作后,代碼都可以正常運行)
技巧02:@Retention 用來聲明注解的保留期限
/** Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*********************/package java.lang.annotation;/*** Annotation retention policy. The constants of this enumerated type* describe the various policies for retaining annotations. They are used* in conjunction with the {@link Retention} meta-annotation type to specify* how long annotations are to be retained.** @author Joshua Bloch* @since 1.5*/ public enum RetentionPolicy {/*** Annotations are to be discarded by the compiler.*/SOURCE,/*** Annotations are to be recorded in the class file by the compiler* but need not be retained by the VM at run time. This is the default* behavior.*/CLASS,/*** Annotations are to be recorded in the class file by the compiler and* retained by the VM at run time, so they may be read reflectively.** @see java.lang.reflect.AnnotatedElement*/RUNTIME } RetentionPolicy.java技巧03:@Target 用來聲明使用該注解的目標類型
/** Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*********************/package java.lang.annotation;/*** The constants of this enumerated type provide a simple classification of the* syntactic locations where annotations may appear in a Java program. These* constants are used in {@link Target java.lang.annotation.Target}* meta-annotations to specify where it is legal to write annotations of a* given type.** <p>The syntactic locations where annotations may appear are split into* <em>declaration contexts</em> , where annotations apply to declarations, and* <em>type contexts</em> , where annotations apply to types used in* declarations and expressions.** <p>The constants {@link #ANNOTATION_TYPE} , {@link #CONSTRUCTOR} , {@link* #FIELD} , {@link #LOCAL_VARIABLE} , {@link #METHOD} , {@link #PACKAGE} ,* {@link #PARAMETER} , {@link #TYPE} , and {@link #TYPE_PARAMETER} correspond* to the declaration contexts in JLS 9.6.4.1.** <p>For example, an annotation whose type is meta-annotated with* {@code @Target(ElementType.FIELD)} may only be written as a modifier for a* field declaration.** <p>The constant {@link #TYPE_USE} corresponds to the 15 type contexts in JLS* 4.11, as well as to two declaration contexts: type declarations (including* annotation type declarations) and type parameter declarations.** <p>For example, an annotation whose type is meta-annotated with* {@code @Target(ElementType.TYPE_USE)} may be written on the type of a field* (or within the type of the field, if it is a nested, parameterized, or array* type), and may also appear as a modifier for, say, a class declaration.** <p>The {@code TYPE_USE} constant includes type declarations and type* parameter declarations as a convenience for designers of type checkers which* give semantics to annotation types. For example, if the annotation type* {@code NonNull} is meta-annotated with* {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull}* {@code class C {...}} could be treated by a type checker as indicating that* all variables of class {@code C} are non-null, while still allowing* variables of other classes to be non-null or not non-null based on whether* {@code @NonNull} appears at the variable's declaration.** @author Joshua Bloch* @since 1.5* @jls 9.6.4.1 @Target* @jls 4.1 The Kinds of Types and Values*/ public enum ElementType {/** Class, interface (including annotation type), or enum declaration */TYPE,/** Field declaration (includes enum constants) */FIELD,/** Method declaration */METHOD,/** Formal parameter declaration */PARAMETER,/** Constructor declaration */CONSTRUCTOR,/** Local variable declaration */LOCAL_VARIABLE,/** Annotation type declaration */ANNOTATION_TYPE,/** Package declaration */PACKAGE,/*** Type parameter declaration** @since 1.8*/TYPE_PARAMETER,/*** Use of a type** @since 1.8*/TYPE_USE } ElementType.java坑01:自定義注解允許定義成員,但是這里的成員在進行聲明時必須是無入參、無拋出異常的方式進行聲明;而且成員只能是方法
坑02:在給成員指定默認值是必須使用default關鍵字
package cn.test.demo.base_demo.annotations;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface NeedTest {boolean value() default true; }1.2 使用注解
1.2.1 創建一個SrpingBoot項目
下載地址:點擊前往
1.2.2 新建一個自定義注解類
package cn.test.demo.base_demo.annotations;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface NeedTest {boolean value() default true; } NeedTest.java1.2.3 新建一個服務類
在該服務類的方法中使用剛剛創建的自定義注解
package cn.test.demo.base_demo.service;import cn.test.demo.base_demo.annotations.NeedTest; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;/*** @author 王楊帥* @create 2018-04-29 21:31* @desc 學生服務類**/ @Service @Slf4j public class StudentService {private final String className = getClass().getName();@NeedTest()public void insert() {log.info("===/" + className + "/insert===新增操作");}@NeedTest(value = false)public void delete() {log.info("===/" + className + "/delete===刪除操作");}} StudentService.java1.2.4 訪問注解
從Java5開始包、類、構造器、方法、字段等都反射對象都開始支持訪問注解信息的方法
/*** {@inheritDoc}* @throws NullPointerException {@inheritDoc}* @since 1.5*/public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {return super.getAnnotation(annotationClass);} package cn.test.demo.base_demo.service;import cn.test.demo.base_demo.annotations.NeedTest; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import java.lang.reflect.Method;import static org.junit.Assert.*;@RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class StudentServiceTest {private final String className = getClass().getName();@Testpublic void test01() {Class cla = StudentService.class;Method[] methods = cla.getDeclaredMethods();log.info("===/" + className + "/test01===方法數量為:{}", methods.length);for (Method method : methods) {NeedTest nt = method.getAnnotation(NeedTest.class);if (nt.value()) {log.info("===" + method.getName() + "()需要測試");} else {log.info("===" + method.getName() + "()不需要進行測試");}}}@Testpublic void insert() throws Exception {}@Testpublic void delete() throws Exception {}} StudentServiceTest.java?
?
轉載于:https://www.cnblogs.com/NeverCtrl-C/p/8973014.html
總結
以上是生活随笔為你收集整理的SpringAOP02 自定义注解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uc浏览器如何取消国家禁止访问
- 下一篇: JS 代码优化