spring中的ReflectionUtils类

来源:互联网 发布:石头人升级数据 编辑:程序博客网 时间:2024/06/05 04:33

今天在看开源软件的代码的时候,看到下面的一段代码:但是没有看懂什么意思,于是加了一个打印,写了一个单元测试,看看它输出的结果:


ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
//System.out.println("------------------"+method.getName());
if (annotation != null) {   Assert.isNull(annotatedMethod.get(), "found more than one method on target class [" + clazz + "] with the annotation type [" + annotationType + "]");   annotatedMethod.set(method);} }})

从输出的结果可以看出,该方法是获取一个类的所有的方法,每获取一个方法的时候,就会回调一个方法,在doWith方法里面找这个方法上是否有对应的注解。ReflectionUtils类是spring-core这个包下的类。笔者看的是4.3.9这个版本。