在具有运行时保留的对象上找不到注释
好吧,我在这里有点困惑。我试图通过在模型上使用注释来选择“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题。这是一个类路径问题。我有一只耳朵,里面有罐子和战争。模型在 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.