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

发布于 2024-12-28 07:43:15 字数 1695 浏览 1 评论 0原文

好吧,我在这里有点困惑。我试图通过在模型上使用注释来选择“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 {
    [...]
}

这是注释定义:

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

我使用以下代码返回正确的“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();
  }
}

但是,当我提供 DispatcherSet< /code> 对象注释始终为空:

10:33:38,498 [INFO] [STDOUT] 类模型不存在注释。DispatcherSet

我在这里遗漏了什么吗?

编辑:

好的,发现了一些有趣的东西,我在 JBoss 容器内运行此代码,当我打印出所有注释时:

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

其中之一应该是我猜测的 DAOSelector 注释的代理实例,所以这可能就是为什么 getAnnotation(DAOSelector.class) 不起作用的原因,请检查一下。

edit2:

不,它们不是 DAOSelector 的实例

Ok, I'm a little bit confused here. I'm trying to select a "DAO" class by using an annotation on the model:

@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:

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:

10:33:38,498 [INFO] [STDOUT] Annotation not present for class 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:

{{{
$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.

edit2:

Nope, they are not an instance of DAOSelector

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2025-01-04 07:43:15

我已经解决了这个问题。这是一个类路径问题。我有一只耳朵,里面有罐子和战争。模型在 jar 中,注释也存在于两者中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文