Java 获取自定义注解 字段值 及 注解值

自定义注解

package com.jianmu.bean.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author kong
 * Retention RetentionPolicy.RUNTIME 运行时
 * Target ElementType.FIELD 属性
 */
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Grade {
    String value() default "";

    int number() default 0;
}

在属性上加注解

package com.jianmu.bean.member.act.config;

import com.alibaba.fastjson.JSONArray;
import com.jianmu.bean.annotation.Grade;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotNull;

/**
 * @author kong
 */
@Data
@Accessors(chain = true)
public class ActUpdateConfigAdParam {
    @NotNull(message = "活动id不能为空")
    private Long aid;

    @ApiModelProperty("启用开屏")
    @Grade("openScreenEnabled")
    private Boolean openScreenEnabled;

    @ApiModelProperty("开屏跳转方式 0 自动 1点击")
    private Integer openScreenJumpMode;

    @ApiModelProperty("开屏图片地址")
    private String openScreen;
    @Grade("voteAdEnabled")
    @ApiModelProperty("启用投票广告")
    private Boolean voteAdEnabled;

    @ApiModelProperty("投票广告图片地址")
    private String voteAd;

    @ApiModelProperty("投票成功跳转地址")
    private String voteAdJumpLink;
    @Grade("bottomAdEnabled")
    @ApiModelProperty("启用底部广告")
    private Boolean bottomAdEnabled;

    @ApiModelProperty("底部广告图地址")
    private String bottomAd;

    @ApiModelProperty("底部广告跳转地址")
    private String bottomAdJumpLink;

    @Grade("vtCarouselAdEnabled")
    @ApiModelProperty("投票页轮播图广告启用")
    private Boolean vtCarouselAdEnabled;

    @Grade("dynamicCarouselAdEnabled")
    @ApiModelProperty("动态页轮播图广告启用")
    private Boolean dynamicCarouselAdEnabled;

    @ApiModelProperty("轮播图广告数组")
    private JSONArray carouselAd;
}

通过反射 循环获取注解在属性中的值,获取字段值

	@GetMapping("/test")
    @ApiResult
    public void test(@RequestBody GradeParam param) throws IllegalAccessException {
        Field[] fields = param.getClass().getDeclaredFields();
        for (Field field : fields) {
            //设置可访问权限
            field.setAccessible(true);
            //获取指定注解
            Grade grade = field.getDeclaredAnnotation(Grade.class);
            //获取注解值
            String v = grade.value();
            //获取属性值
            Object o = field.get(param);
            log.debug("注解值:{} 属性值:{}", v, o);
        }

    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中,我们可以通过自定义注解来修改字段。首先,我们需要定义一个注解。可以使用@interface关键字来定义注解。例如,假设我们要定义一个名为@CustomAnnotation的注解,该注解用于修改字段。 ```java 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.FIELD) public @interface CustomAnnotation { String value() default ""; } ``` 在注解内部,使用了@Retention和@Target元注解来指定注解的保留策略和注解的作用目标。在此例中,我们设置了@Retention(RetentionPolicy.RUNTIME),表示该注解在运行时可见。@Target(ElementType.FIELD)表示该注解可以用于字段上。 接下来,我们可以将@CustomAnnotation应用到字段上,并使用反射机制获取字段并修改其。 ```java import java.lang.reflect.Field; public class DemoClass { @CustomAnnotation("Hello World") private String value; public void setValueUsingAnnotation() throws IllegalAccessException { Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(CustomAnnotation.class)) { CustomAnnotation annotation = field.getAnnotation(CustomAnnotation.class); field.setAccessible(true); field.set(this, annotation.value()); } } } public static void main(String[] args) throws IllegalAccessException { DemoClass demo = new DemoClass(); System.out.println("Before modification: " + demo.value); demo.setValueUsingAnnotation(); System.out.println("After modification: " + demo.value); } } ``` 在上述示例中,我们定义了一个名为DemoClass的类,并在其中声明了一个私有字段value。我们将@CustomAnnotation应用到该字段上,并在注解中设置了一个字符串。 在setValueUsingAnnotation方法中,使用反射机制获取类的所有字段。然后,检查每个字段是否应用了@CustomAnnotation注解。如果有,则使用Field类的setAccessible方法来设置字段可访问,并使用Field类的set方法将注解中的赋给字段。 在main方法中,我们创建了DemoClass的实例,并输出修改前后的字段。 通过运行上述代码,我们可以看到输出结果中,字段在应用自定义注解后发生了变化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等一场春雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值