Java使用Annotation创建类的实例

时间:2015-04-02 11:22:13

标签: java annotations

我正在尝试使用annotation创建一个类的实例。让我说我有两个来自父类的子类

public class Parent{

}

@(type = abc)
public class Child1 extends Parent{


}

@(type = def)
public class Child2 extends Parent{


}

现在,我想要一个方法,当我传递“def”时,当我传递“abc”时会返回类child1的实例,而当我传递“def”时,它会返回类child2的实例。 是否可以使用注释。

1 个答案:

答案 0 :(得分:1)

您需要遍历所有类并找到具有感兴趣注释的类。然后,您可以为类创建“类型”索引,以便稍后查找以确定要创建的类。

您可以使用Reflections库查找具有注释的所有类。