首页 > 解决方案 > 如何使用注释正确地从 bean 中提取字段

问题描述

我有一个带有注释的 getter 和字段的 bean:

public class CaseSimpleObjGetter {
    private int a;

    @Hashed
    private int b;

    private int c;

    CaseSimpleObjGetter(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Hashed
    public int getA() {
        return a;
    }

    protected int getB() {
        return b;
    }

    protected int getC() {
        return c;
    }
}

我想实现一个提取器,它将从类似对象中提取所有可访问的字段。
特别是,我想访问注释信息。
我想知道,实现该提取器的最佳方法是什么?
在此示例中,我想提取 (a, b, c) 的名称和值,并知道哪些使用 @Hashed 注释进行了注释。

我以前使用过 BeanWrapperImpl(来自 spring)和 Introspector。
但是,我找不到从属性访问注释的方法。
bean 中实际上是否允许注释?

标签: javaannotationsjavabeans

解决方案


推荐阅读