Annotation.properties这个配置文件里的实体类名。className = User//这是User这个实体类,上面有个自定义的Table注解
@Table(name="tableName")
public class User {
private String name;
private String password;
private int age;

public User() {
super();
// TODO Auto-generated constructor stub
}
………//这是Table注解的内容
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Table{
String name();
}
public class tableTest{
public static void main(String[] args) throws Exception{

//读取配置文件
InputStream ips = tableTest.class.getResourceAsStream("/Annotation.properties");
Properties pt = new Properties();
pt.load(ips);
ips.close();
//从配置文件获取实体类名
String className = pt.getProperty("className");
Class clazz = Class.forName(className);
if(clazz.isAnnotationPresent(Table.class)){
//得到该实体类的注解
Annotation annotation = clazz.getAnnotation(Table.class);
System.out.println(annotation);
}else{
System.out.println("错误");
}
}
}
问题是这样的,我想运用annotation调用Table注解里的name方法,可.了后没有提示。

解决方案 »

  1.   

    这个事 注解 类似 配置参数的 可以得到 注解里面name 的 值,
    不是有 getValue这个方法吗?
        
      

  2.   

    我本机测试没有问题,输出:
    @cn.leisore.daily._2011_05_01.Table(name=tableName)检查你的配置的文件中的类名是否是全限定名,即是否漏掉了包名
      

  3.   

    我也能输出你的那个结果。
    问题是我想运用annotation调用Table注解里的name方法,即:annotation.name,单独输出tableName
    但我输入annotation.后 没有name方法提示,不能使用。
      

  4.   

    这个你获取到注解需要下转成具体的注解类型才可以 if(clazz.isAnnotationPresent(Table.class)){
                //得到该实体类的注解
                Table annotation = (Table) clazz.getAnnotation(Table.class);
                System.out.println(annotation.name());
            }