【问题标题】:Error message "cvc-complex-type.4: Attribute 'bean-discovery-mode' must appear on element 'beans'."错误消息“cvc-complex-type.4:属性 'bean-discovery-mode' 必须出现在元素 'beans' 上。”
【发布时间】:2014-12-28 11:12:45
【问题描述】:

我有一个简单的 Java EE 项目,在 WEB-INF 下有一个简单的 beans.xml。它基本上什么都没有。但我总是收到此错误消息“cvc-complex-type.4:属性'bean-discovery-mode'必须出现在元素'beans'上。” JBoss Developer Studio 中的“http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd”行。

这条消息是什么意思?我用谷歌搜索但一无所获。什么可能导致这种情况?

<?xml version="1.0" encoding="UTF-8"?>
<!-- Marker file indicating CDI should be enabled -->
<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">

    <!-- Uncomment this alternative to see EJB declarative transactions in 
        use -->
    <!-- <alternatives> -->
        <!-- <class>org.jboss.as.quickstarts.greeter.domain.EJBUserDao</class> -->
    <!-- </alternatives> -->
</beans>

【问题讨论】:

  • 这个错误是只出现在你的IDE还是你的应用服务器?

标签: jakarta-ee jboss cdi


【解决方案1】:

消息清楚地表明属性bean-discovery-mode 应该存在于元素beans 中,这可能是您的容器的要求。尝试将属性赋予 bean 元素,如下所示:

bean-discovery-mode = "annotated"

原因是您使用的是 CDI 1.1,因此bean-discovery-mode 是强制属性。您可以通过阅读以下 URL 提供的 XSD 文件来确认:

http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd

请特别注意以下部分:

<xs:attribute name="bean-discovery-mode" use="required">
            <xs:annotation>
                <xs:documentation>
                   It is strongly recommended you use "annotated". 

                   If the bean discovery mode is "all", then all types in this
                   archive will be considered. If the bean discovery mode is
                   "annotated", then only those types with bean defining annotations will be
                   considered. If the bean discovery mode is "none", then no
                   types will be considered.
                </xs:documentation>
            </xs:annotation>

【讨论】:

  • 是的,那工作。但是这个例子是随 Jboss Wildfly 快速启动项目一起提供的。我想它应该可以在没有任何修改的情况下工作。为什么它不起作用?
  • 该示例可能是为 CDI 1.0 编写的,而您似乎使用的是 CDI 1.1。请参阅我的更新答案。
猜你喜欢
  • 2012-10-25
  • 2014-03-10
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多