将 @Inject 与泛型类型一起使用
我一直在这里搜索它,但还没有找到答案。
在我的应用程序中,我的控制器有一个抽象主类,其中包含一些方法和属性。我想自动注入 DAO。
abstract class AbstractController<E extends AbstractEntity, D extends AbstractDAO<E>> {
@Inject
private D dao;
// getters and setters
}
abstract class AbstractDAO<E extends AbstractEntity> {
@PersistentContext
private EntityManager em;
// finds returns E
}
// implemenation/usage
class CarController extends AbstractController<Car, CarDAO> {
}
获取异常:
org.jboss.weld.exceptions.DefinitionException: WELD-001407 Cannot declare an injection point with a type variable: [field] @Inject private AbstractController.dao
使用:Glassfish 3.1 和 JSF 2.1。
有解决方法或替代方案吗?
谢谢。
I've been searching here about it, but haven't found an answer.
In my application, I've an abstract main class for my controllers, with some methods and properties. And I want to inject the DAO automatically.
abstract class AbstractController<E extends AbstractEntity, D extends AbstractDAO<E>> {
@Inject
private D dao;
// getters and setters
}
abstract class AbstractDAO<E extends AbstractEntity> {
@PersistentContext
private EntityManager em;
// finds returns E
}
// implemenation/usage
class CarController extends AbstractController<Car, CarDAO> {
}
Getting the exception:
org.jboss.weld.exceptions.DefinitionException: WELD-001407 Cannot declare an injection point with a type variable: [field] @Inject private AbstractController.dao
Using: Glassfish 3.1 and JSF 2.1.
Is there a workaround or alternative for this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从技术上来说,反射通过源中的泛型声明检测正确的运行时类型并转换为它是非常复杂的。 Weld 根本不支持也不会支持它。
最好针对
AbstractDAO
声明它:无论如何,针对
D
声明它都不会带来任何好处。It's technically very complicated for reflection to detect the proper runtime type by a generic declaration in the source and cast to it. Weld simply don't and won't support it.
Better declare it against
AbstractDAO<E>
:You gain nothing with declaring it against
D
anyway.Weld 是 CDI 规范的实现,并且注入点中的类型变量是被禁止的
如果你不关心CDI规范,你可以看看这个焊接 PR。
Weld is an implementation of the CDI specification and type variables in injection points are forbidden by the specification.
If you don't care CDI specification,you can take look this weld PR.