java CDI - 正确的 bean.xml 格式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32942876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:59:18  来源:igfitidea点击:

CDI - what is the correct bean.xml format?

javaglassfishcdijboss-weld

提问by Ralph

I have a question about the correct format and usage of the bean.xml file. In my projects I typically used this content for my bean.xml files (no explizit bean declaration used):

我有一个关于 bean.xml 文件的正确格式和用法的问题。在我的项目中,我通常将此内容用于我的 bean.xml 文件(未使用 explizit bean 声明):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

This works well in WildFly 8 and 9. But I have deployment issues in GlassFish 4. In the question: Glassfish 4, simple example in CDI fails with WELD-001408 Unsatisfied dependenciesI wrote about an alternative format:

这在 WildFly 8 和 9 中运行良好。但是我在 GlassFish 4 中遇到了部署问题。在问题中:Glassfish 4,CDI 中的简单示例失败,WELD-001408 不满意的依赖项我写了关于替代格式的内容:

<beans
   xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                  http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
          bean-discovery-mode="all">
</beans>

There a different namespaces used. And GlassFish4 seems to care about that.

使用了不同的命名空间。GlassFish4 似乎很在意这一点。

What is the correct format of an empty bean.xml File used for JEE7 ?

用于 JEE7 的空 bean.xml 文件的正确格式是什么?

回答by G. Demecki

Correct empty beans.xmlcan be totally emptyfile, really ;-)

正确的空beans.xml可以是完全空的文件,真的;-)

But when you want to add some content, please notice that most of the XML deployment descriptor namespaces have been updated in Java EE 7. This post describesthe details. Also bean-discovery-modehas been added.

但是当您想要添加一些内容时,请注意大部分 XML 部署描述符命名空间已在 Java EE 7 中更新。这篇文章描述了详细信息。也bean-discovery-mode已添加。

BTW: Sample beans.xmlwhich I'm using right now looks like:

顺便说一句:beans.xml我现在使用的示例看起来像:

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.2" bean-discovery-mode="annotated">

    <!-- some content -->
</beans>

You may notice the usage of version="1.2"attribute - you can freely set it to 1.1. It just serves as a reminder to the reader that project is using CDI 1.2 (which in fact is just a Maintenance releaseof the CDI 1.1Specification).

您可能会注意到version="1.2"属性的用法- 您可以自由地将其设置为1.1. 它只是作为一个提醒读者,项目是使用CDI 1.2(这其实只是一个维护版本中的CDI 1.1规范)。