Java 为什么我不能';t获取字段';s注释?

Java 为什么我不能';t获取字段';s注释?,java,reflection,annotations,Java,Reflection,Annotations,这是我自己的注解: import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.SOURCE) public @interfac

这是我自己的注解:

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

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface Autowire {
    public String id();
}
@Component(id="businessObject")
public class BusinessObject {

    @Autowire(id="dataAccessInterface")
    private DataAccessInterface dai;

    public void print() {
        System.out.println(dai.queryFromTableA());
    }

    public void setDai(DataAccessInterface dai) {
        this.dai = dai;
    }
}
Autowire at = f.getAnnotation(Autowire.class);
然后,我定义了一个类,并使用注释设置了一个属性:

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

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface Autowire {
    public String id();
}
@Component(id="businessObject")
public class BusinessObject {

    @Autowire(id="dataAccessInterface")
    private DataAccessInterface dai;

    public void print() {
        System.out.println(dai.queryFromTableA());
    }

    public void setDai(DataAccessInterface dai) {
        this.dai = dai;
    }
}
Autowire at = f.getAnnotation(Autowire.class);
@组件也是我的

毕竟,我定义了一个跟踪器:

public class BeanFactory {

    private HashMap<String, Object> beanPool;
    private HashMap<String, String> components;

    public BeanFactory() {
        beanPool = new HashMap<>();

        scanComponents();
    }

    private void scanComponents() {
        components = ComponentScanner.getComponentClassName("com.oolong.javase.annotation");
    }

    public Object getBean(String id) throws ClassNotFoundException, 
            InstantiationException, IllegalAccessException, NoSuchMethodException, 
            SecurityException, IllegalArgumentException, InvocationTargetException {

        if (beanPool.containsKey(id)) {
            return beanPool.get(id);
        }

        if (components.containsKey(id)) {

            Object bean = Class.forName(components.get(id)).newInstance();

            bean = assemblyMember(bean);

            beanPool.put(id, bean);

            return getBean(id);
        }

        throw new ClassNotFoundException();
    }

    private Object assemblyMember(Object obj) throws ClassNotFoundException, 
            InstantiationException, IllegalAccessException, NoSuchMethodException, 
            SecurityException, IllegalArgumentException, InvocationTargetException {

        Class cl = obj.getClass();

        for (Field f : cl.getDeclaredFields()) {
            Autowire at = f.getAnnotation(Autowire.class);

            if (at != null) {

                Method setMethod = cl.getMethod("set" + captureName(f.getName()), f.getType());
                setMethod.invoke(obj, getBean(at.id()));
            }
        }

        return obj;
    }

    public static String captureName(String name) {
        char[] cs=name.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
    }
}
位于
null

为什么??
(对不起,我英语不好!)

您正在使用
源代码
保留策略。sdk的引用:

/**
 * Annotations are to be discarded by the compiler.
 */
请尝试改用
运行时
保留策略:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowire {
    public String id();
}
根据文件:

/**
 * 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.
 *