java ee6:覆盖CDI替代 [英] java ee6: override CDI alternative

查看:114
本文介绍了java ee6:覆盖CDI替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Glassfish 3.1.2.2,java ee6。

我有一个库,其中一个类使用CDI来获得一个辅助类。我想在一个使用该库的特定项目中重写该CDI依赖项,并强制该库使用我自己的帮助器类,而不是该项目特有的帮助器类。我可以随意修改这个库,但默认情况下它应该使用它的默认助手类,这样行为不会改变为库的其他用户。



这应该成为 @Alternative CDI模式的完美应用。我为辅助类API创建了一个java接口;在库中有一个默认实现,那么我可以在 beans.xml 中使用< alternatives>

除了它没有工作。它的显然 不可能,以覆盖CDI 1.0(java ee6)库外的库中的替代行为。

等等无论我在外部项目的beans.xml中指定了什么,CDI都会选择在库中定义的bean。



我认为通过制作人,但我没有找到如何让CDI将EntityManager作为参数提供给制作人,以便我可以将它传递给助手类。在这个项目中,我们通常使用 @PersistenceContext 注释来注入EntityManager。



关于如何覆盖从外部项目CDI注入?

解决方案

我最终使用的解决方案与LightGuard的建议有关:我有CDI然后我重写processAnnotatedType(),就像建议的那样。然而,不是用 AnnotatedType 我不知道该怎么做),我使用了那里描述的技术:
http://docs.jboss.org/weld/reference/latest/en-US/html/extend.html#d0e4800



并否决库中定义的bean。

现在默认的实现被否决了,如果我把自己的bean放在我的应用程序,这是CDI选择的应用程序。



为了使这项工作也在Arquillian集成测试,您必须将此调用添加到您的存档:

  addAsServiceProvider(Extension.class,< CDI extension class name> .class)

javax.enterprise.inject.spi.Extension 资源不需要在Arquillian中有效(请参阅 SHRINKWRAP-266 )。


I'm using Glassfish 3.1.2.2, java ee6.

I have a library in which a class uses CDI to get a helper class. I would like in one specific project where I use that library, to override that CDI dependency and force the library to use my own helper class, specific to that project instead. I can modify the library at will, but by default it should use its default helper class, so that behaviour doesn't change for other users of the library.

This should be the perfect application of the @Alternative CDI pattern. I made a java interface for the helper class API; There is a default implementation in the library, then I can use the <alternatives> tag in the beans.xml; in the project where I want to override the behaviour, I would specify my own implementation of the helper in the beans.xml of that specific project.

Except it doesn't work. It's apparently impossible to override the alternatives behaviour from the library outside of the library in CDI 1.0 (java ee6).

And so no matter what I specify in the beans.xml of my outer project, CDI keeps selecting the bean defined in the library.

I considered going through a producer but I didn't find how to get CDI to give to the producer the EntityManager as a parameter, so that I can pass it forward to the helper class. In this project, we normally inject the EntityManager using the @PersistenceContext annotation.

Any ideas on how to override that CDI injection from the outer project?

解决方案

The solution I used in the end is related to the suggestion from LightGuard: I have CDI extension, and I override processAnnotatedType(), as suggested.

However instead of replacing the AnnotatedType (which I'm not sure how I could do), I use the technique described there: http://docs.jboss.org/weld/reference/latest/en-US/html/extend.html#d0e4800

And veto the the bean that was defined in the library.

Now that the default implementation is veto'ed, if I put my own bean in my application, that's the one that CDI chooses.

To make this work also in Arquillian integration tests, you must add this call to your archive:

addAsServiceProvider(Extension.class, <CDI extension class name>.class)

The javax.enterprise.inject.spi.Extension resource does not need to have effect in Arquillian (see SHRINKWRAP-266).

这篇关于java ee6:覆盖CDI替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆