日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringAOP02 自定义注解

發布時間:2023/12/2 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.java

    1.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.java

    1.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 自定义注解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。