焊接中外部(第 3 方)bean 的解析

发布于 2024-08-21 18:03:54 字数 476 浏览 10 评论 0原文

我知道它仍然不太受欢迎,因为该规范几个月前才发布。

我还没有“安装”焊接,我只是在阅读,通过这个问题,我想确保我已经正确理解了这一重要点:

是否通过将第三方 jar 中的 bean 声明为来实现解析 在您的 beans.xml 中?

如果没有,如何使用没有 beans.xml 的第三方库中的 bean?

除非 META-INF 中有 beans.xml ,否则将 jar 放在类路径中将不起作用,而对于 3rd 方 jar 则无法实现这一点。 (请参阅 Gavin King 关于该主题的帖子

I know it is still not quite popular, since the spec was released just a few months ago.

I haven't "installed" weld yet, I'm just reading, and by this question I want to make sure I've understood this important point correct:

Is resolution of beans that are in 3rd-party jars achieved by declaring them as <alternatives> in your beans.xml?

If not, how to use beans from 3rd party libraries that don't have beans.xml ?

Putting the jar on the classpath won't work unless there is beans.xml in their META-INF, which you can't make true for 3rd party jars. (see Gavin King's post on the subject)

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

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

发布评论

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

评论(2

儭儭莪哋寶赑 2024-08-28 18:03:54

为什么想得这么复杂?

只需为那些第 3 方类创建一个 ProducerMethod 即可。

假设您有一个第三方库,它会自动获取 PDF 文件并按传真发送它们,并且您喜欢

private @Inject PdfFaxService faxService;

在代码中使用类似的内容,那么您可以简单地使用生成器方法来提供它。 PdfFaxService 工作无状态,因此我们可以放心地假设我们可以将其设为 @ApplicationScoped:

public @Produces @ApplicationScoped PdfFaxService createFaxService() {
  return new PdfFaxService(initparameters);
}

某处。

嗯。

why think so complicated?

Simply make a producerMethod for those 3rd party classes.

Let's assume you have a 3rd party library which automatically takes PDF files and sends them per faximile, and you like to use something like

private @Inject PdfFaxService faxService;

in your code, then you could simply provide this with a producer method. The PdfFaxService works stateless, so we can safely assume that we can make it @ApplicationScoped:

public @Produces @ApplicationScoped PdfFaxService createFaxService() {
  return new PdfFaxService(initparameters);
}

somewhere.

hth.

许仙没带伞 2024-08-28 18:03:54

我对替代方案的理解是,它是可以在不同的部署环境(例如测试环境)中使用的接口的其他实现的替代方案。 alternative bean 是通过使用 @Alternative 注释来声明的。

要在给定部署场景中使用替代方案,您可以在 CDI 部署描述符 META-INF/beans.xml 元素中选择它。这将启用默认情况下禁用的@Alternative bean。

启用后,如果容器发现给定注入点的不明确依赖项,它将查看可以注入的替代方案,如果恰好有一个,则选择该替代方案。

换句话说,替代是在部署时用另一个实现替换现有实现的好方法。如果没有什么可替换的,则不需要替代方案,只需将 jar 放在类路径上即可。虽然不确定这正是你的问题,但我对第 3 方 jar 的概念有疑问。

更多信息请参见 2.1.4。替代方案4.6。替代方案4.7。修复不满意和不明确的依赖项(但我猜这就是您正在阅读的内容)。

更新:回答您的其他问题。

如果没有,如何使用没有 beans.xml 的第三方库中的 beans

这不可能发生,bean 存档必须有一个 bean.xml (可以是空的),详细信息请参阅15.6 节。文档的打包和部署

CDI 没有定义任何特殊的
部署存档。您可以打包
JAR、EJB-JAR 或 WAR 中的 bean——任何
应用程序中的部署位置
类路径。但是,存档必须
成为“bean 档案”。这意味着每个
包含 beans 的存档必须
包含一个名为 beans.xml 的文件
类路径的 META-INF 目录或
Web 根目录的 WEB-INF 目录(用于
战争档案)。该文件可能为空。
部署在不存在的档案中的 Bean
不会有 beans.xml 文件
可在应用程序中使用。

然后,要修复不满足且不明确的依赖关系,请参阅前面提到的 4.7 节。

更新2:使用BeforeBeanDiscovery.addAnnotatedType()似乎可以添加在bean发现期间要考虑的其他类。 (BeforeBeanDiscovery 是一个事件)

My understanding of an alternative is that it's an alternative to some other implementation of an interface that you can use in a different deployment environment (e.g. a testing environment). An alternative bean is declared by annotating it with @Alternative.

To use an alternative in a given deployment scenario, you select it in the <alternatives> element of your CDI deployment descriptor META-INF/beans.xml. This will enable @Alternative beans which are disables by default.

When enabled, if the container finds an ambiguous dependency for a given injection point, it will look at alternatives that could be injected and, if there is exactly one, pick up this alternative.

In other words, alternatives are a nice way to replace an existing implementation with another one at deployment time. If there is nothing to replace, you don't need alternatives, just put your jar on the class path. Not sure this was exactly your question though, I have a doubt about the concept of 3rd-party jars.

More in 2.1.4. Alternatives, 4.6. Alternatives and 4.7. Fixing unsatisfied and ambiguous dependencies (but I guess that this is what you're reading).

Update: To answer your additional question.

If not, how to use beans from 3rd party libraries that don't have beans.xml

This can't happen, a bean archive must have a bean.xml (be it empty) as detailed in the section 15.6. Packaging and deployment of the documentation:

CDI doesn't define any special
deployment archive. You can package
beans in JARs, EJB-JARs or WARs—any
deployment location in the application
classpath. However, the archive must
be a "bean archive". That means each
archive that contains beans must
include a file named beans.xml in the
META-INF directory of the classpath or
WEB-INF directory of the web root (for
WAR archives). The file may be empty.
Beans deployed in archives that do not
have a beans.xml file will not be
available for use in the application.

Then, to fix an unsatisfied and ambiguous dependency, refer to the section 4.7 previously mentioned.

Update 2: It appears that using BeforeBeanDiscovery.addAnnotatedType() it is possible to add other classes to be taken into consideration during bean discovery. (BeforeBeanDiscovery is an event)

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