Mat*_*ies 6 java reflection annotations
好的,我在这里有点困惑.我试图通过在模型上使用注释来选择"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 {
[...]
}
Run Code Online (Sandbox Code Playgroud)
这是注释定义:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DAOSelector {
Class<?> dao();
}
Run Code Online (Sandbox Code Playgroud)
我使用以下代码返回正确的"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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我提供DispatcherSet对象时,注释始终为null:
10:33:38,498 [INFO] [STDOUT]类model.DispatcherSet不存在注释
我在这里错过了什么吗?
编辑:
好的,发现了一些有趣的东西,我在JBoss容器中运行这段代码,当我打印出所有的注释时:
{{{
$Proxy76
$Proxy708
$Proxy77
}}}
Run Code Online (Sandbox Code Playgroud)
其中一个应该是DAOSelector我猜测的注释的代理实例,所以这可能是为什么getAnnotation(DAOSelector.class)不起作用,检查出来.
EDIT2:
不,他们不是一个例子 DAOSelector
| 归档时间: |
|
| 查看次数: |
3056 次 |
| 最近记录: |