请大家先看看这个文章[url]http://www.iteye.com/topic/1116083[/url]
这里面写的两个类,如果单独测试没有问题 java.lang.annotation.Annotation annotation = cls.getAnnotations()[0]; 这行代码能返回annotation 。如果把这两个类放到web项目 中! 与struts和spring结合。我在action的时候注入了User这个对象,可是通过.getClass().getAnnotations();就得不到对象了。这样的话我调用ControlUtil这个类来想返回一条insert sql就没用了。
请教个位这是什么情况。
楼主可以把这个代码跑一下看看结果就知道怎么回事了
[code="java"]
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
public class Main {
/**
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void main(String[] args) throws SecurityException,
NoSuchMethodException {
Parent p = new Parent();
Method pMethod = p.getClass().getMethod("aMethod");
printAnnotation(pMethod);
Child c = new Child();
Method cMethod = c.getClass().getMethod("aMethod");
printAnnotation(cMethod);
}
private static void printAnnotation(Method method) {
System.out.println(method);
if (method != null) {
for (Annotation a : method.getAnnotations()) {
System.out.println(a);
}
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MethodAnnotation {
};
class Parent {
@MethodAnnotation
public void aMethod() {
};
}
class Child extends Parent {
public void aMethod() {
};
};
[/code]
这个明显是spring注入的时候放的是动态代理么,应该是你加了拦截器或者事务控制之类的吧。
代理类上找不到,往它的父类找找,肯定能找到你那个类得。