从我正在经历的书中拍摄的图像,
标题说明了一切。请建议或给我一些关于幕后发生的事情。
例如,Hibernate @NotNull
中的Bean Validation API
如何运作?
我知道通过Reflection API,我们可以做这样的事情,
class Meta {
// Annotate a method.
@MyAnno(str = "Annotation Example", val = 100)
public static void myMeth() {
Meta ob = new Meta();
// Obtain the annotation for this method
// and display the values of the members.
try {
// First, get a Class object that represents
// this class.
Class c = ob.getClass();
// Now, get a Method object that represents
// this method.
Method m = c.getMethod("myMeth");
// Next, get the annotation for this class.
MyAnno anno = m.getAnnotation(MyAnno.class);
// Finally, display the values.
System.out.println(anno.str() + " " + anno.val());
} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}
public static void main(String args[]) {
myMeth();
}
}
答案 0 :(得分:1)
注释没有任何已实现的代码,实际上并没有做任何事情。
要使它们工作",应该有某种注释处理器(初始化器,加载器或任何使用带注释对象的类)。此注释处理器检查注释对象注释并更改其处理方式。
例如,Spring注释处理器在初始化对象时,会查找@Autowired字段,以填充自动连接的字段。
同样适用于Hibernates @NotNull。它实际上并没有做任何事情。但是,Hibernate在持久化对象时会检查是否存在某些内容。