SpringBoot?AnnotationUtils工具類的使用實例詳解
更新時間:2022年09月29日 14:26:53 作者:fengyehongWorld
這篇文章主要介紹了SpringBoot?AnnotationUtils工具類的使用,使用自定義注解標記業(yè)務(wù)方法,原生Java獲取注解及AnnotationUtils工具類獲取方法,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
一. 前期準備
?若干自定義注解
import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface PayCode { // 支付方法的業(yè)務(wù)code String payCode(); // 支付方法的名稱 String name(); }
import java.lang.annotation.*; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface PayOrder { int value() default 0; }
import java.lang.annotation.*; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) public @interface Version { String value() default "0"; }
二. 使用自定義注解標記業(yè)務(wù)方法
@PayCode(payCode = "alia", name = "支付寶支付") @Component public class AliaPay { @PayOrder(value = 1) public void pay() { System.out.println("===發(fā)起支付寶支付1==="); } }
@PayCode(payCode = "jingdong", name = "京東支付") @Component public class JingDongPay { @Version(value = "1.1") public String version; @PayOrder(value = 20) public void pay() { System.out.println("===發(fā)起京東支付==="); } }
三. 原生Java獲取注解
import org.springframework.boot.CommandLineRunner; import org.springframework.util.ReflectionUtils; @Component public class ZTestController implements CommandLineRunner { @Resource private AliaPay aliaPay; @Resource private JingDongPay jingDongPay; @Override public void run(String... args) throws Exception { // ?原生Java的方式獲類上的注解 PayCode aliPay = aliaPay.getClass().getAnnotation(PayCode.class); System.out.println(aliPay); // @com.example.jmw.common.annotation.PayCode(payCode=alia, name=支付寶支付) // ?原生Java的方式獲取屬性上的注解 Field versionField = ReflectionUtils.findField(jingDongPay.getClass(), "version"); Version version = versionField.getAnnotation(Version.class); } }
四. AnnotationUtils工具類獲取
4.1 AnnotationUtils.findAnnotation獲取類注解
// AnnotationUtils的方式獲取指定類上的注解 import org.springframework.core.annotation.AnnotationUtils; PayCode aliPay = AnnotationUtils.findAnnotation(aliaPay.getClass(), PayCode.class);
4.2 AnnotationUtils.findAnnotation獲取方法注解
import org.springframework.util.ReflectionUtils; import org.springframework.core.annotation.AnnotationUtils; // 通過反射獲取aliaPay對象上的pay方法的Method對象 Method payMethod = ReflectionUtils.findMethod(aliaPay.getClass(), "pay"); // 獲取方法上的注解 PayOrder payOrder = AnnotationUtils.findAnnotation(payMethod, PayOrder.class);
4.3 AnnotationUtils.getValue獲取注解上的指定屬性值
// AnnotationUtils的方式獲取指定類上的注解 PayCode aliPayAnnotation = AnnotationUtils.findAnnotation(aliaPay.getClass(), PayCode.class); // 獲取注解上指定的值 Object payCode = AnnotationUtils.getValue(aliPayAnnotation, "payCode");
4.4 AnnotationUtils.getAnnotationAttributes獲取注解上的所有屬性值
// 獲取注解上所有的屬性值 PayCode aliPay = AnnotationUtils.findAnnotation(aliaPay.getClass(), PayCode.class); Map<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(aliPay);
到此這篇關(guān)于SpringBoot AnnotationUtils工具類的使用的文章就介紹到這了,更多相關(guān)SpringBoot AnnotationUtils工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring cloud 使用Eureka 進行服務(wù)治理方法
這篇文章主要介紹了spring cloud 使用Eureka 進行服務(wù)治理方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05