简体   繁体   English

在具有运行时保留的对象上找不到注释

[英]Annotation not found on object with Runtime retention

Ok, I'm a little bit confused here. 好的,我在这里有点困惑。 I'm trying to select a "DAO" class by using an annotation on the model: 我试图通过在模型上使用注释来选择“DAO”类:

@Entity
@Table(name="dispatcher")
// use the Kamailio Base DAO for code that supports this annotation
@DAOSelector(dao = DAOBaseKamailio.class) 
public class DispatcherSet extends Model {
    [...]
}

Here is the annotation defenition: 这是注释定义:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DAOSelector {
       Class<?> dao();
}

I use the following code to return the proper "DAO" class: 我使用以下代码返回正确的“DAO”类:

public static DAOInterface getCorrectDAO(final Object object) throws Exception {
  final DAOSelector annotation = 
    object.getClass().getAnnotation(DAOSelector.class);

  if(annotation != null) {
    System.out.println("Annotation present: " + 
      annotation.dao().getName() + " for class " + object.getClass().getName());

    final Object dao = annotation.dao().newInstance();
    if(!(dao instanceof DAOInterface)) {
      throw new Exception("Invalid Base DAO in annotation for entity " + 
        object.getClass().getName());
    }
    return (DAOInterface) dao;
  }
  else {
    System.out.println("Annotation not present for class " + 
      object.getClass().getName());
    return new DAOBase();
  }
}

However, when I feed a DispatcherSet object annotation is always null: 但是,当我提供DispatcherSet对象时,注释始终为null:

10:33:38,498 [INFO] [STDOUT] Annotation not present for class model.DispatcherSet 10:33:38,498 [INFO] [STDOUT]类model.DispatcherSet不存在注释

Am I missing something here? 我在这里错过了什么吗?

edit: 编辑:

OK, found something interesting, I'm running this code inside a JBoss container and when I print out all the annotations: 好的,发现了一些有趣的东西,我在JBoss容器中运行这段代码,当我打印出所有的注释时:

{{{
$Proxy76
$Proxy708
$Proxy77
}}}

One of these should be a proxied instance of the DAOSelector annotation I'm guessing, so that's probably why getAnnotation(DAOSelector.class) won't work, checking it out. 其中一个应该是我猜测的DAOSelector注释的代理实例,所以这可能是为什么getAnnotation(DAOSelector.class)无法工作,检查出来的原因。

edit2: EDIT2:

Nope, they are not an instance of DAOSelector 不,它们不是DAOSelector的实例

I've fixed the problem. 我已经解决了这个问题。 it was a classpath issue. 这是一个类路径问题。 I have an ear containing a jar and war. 我的耳朵里装着一个罐子和战争。 The model was in the jar and the annotation was present in both. 模型在罐子里,两者都有注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM