SpringBoot AnnotationUtils工具类的使用


一. 前期准备

⏹若干自定义注解

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PayCode {

    // 支付方法的业务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";
}

二. 使用自定义注解标记业务方法

@PayCode(payCode = "alia", name = "支付宝支付")
@Component
public class AliaPay {

    @PayOrder(value = 1)
    public void pay() {
        System.out.println("===发起支付宝支付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("===发起京东支付===");
    }
}

三. 原生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);
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值