假设我有一个看起来像这样的 POJO。
public class SomeDataClass {
@SomeProperty1
String myField1;
@SomeProperty1
String myField2;
...
getters()...
setters()... // you know the drill
}
我希望能够向实例化对象综合注入(inject)注释。如果我使用反射来检查新的 POJO,它实际上看起来像这样:
public class SomeDataClass {
@SomeProperty1
String myField1;
@SomeProperty1
--> @SomeProperty2 <--- the logic flow would change depending on whether this annotation is present
String myField2;
...
getters()...
setters()... // you know the drill
}
这可能吗?我怎样才能完成这样的事情?
最佳答案
Annotations只是语法元数据提供者...
因此,您应该通过反射获取给定类中声明的字段的名称,并使用所需的标准来区分其中一个字段需要@SomeProperty2
提供的信息,之后在运行时添加来自@SomeProperty2
的信息只是setter或getter的正常使用...
关于java - 我可以将注释注入(inject)到 pojo 中吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37844887/