请大家先看看这个文章[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就没用了。
请教个位这是什么情况。
java反射问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- easyroom 2011-10-01 11:36关注
楼主可以把这个代码跑一下看看结果就知道怎么回事了
[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]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#Java#的问题,如何解决?
- ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
- ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥15 cmd cl 0x000007b
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
- ¥500 火焰左右视图、视差(基于双目相机)
- ¥100 set_link_state
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化