【问题标题】:CDI and web fragments: not working without beans.xmlCDI 和 web 片段:没有 beans.xml 就无法工作
【发布时间】:2015-06-04 22:11:51
【问题描述】:

我有三个 maven 项目:

  • core (packagetype=JAR) 与 src/main/resources/beans.xml bean-discovery-mode="annotated"
  • webfragment (packagetype=JAR) 与 src/main/resources/beans.xml bean-discovery-mode="annotated"
  • webproject (packagetype=WAR) 与 src/main/webapp/WEB-INF/beans.xml bean-discovery-mode="annotated"

我的类路径中有 org.jboss.weld.servlet:weld-servlet 2.2.9.Final (CDI 1.2) 并使用 tomcat 7.0.55。此外,我使用 Servlet 3、Deltaspike 1.2.1、Mojarra 2.2.10。

所有我的 CDI bean 都有一个 bean 定义注释(请参阅 Meaning of bean discovery mode annotated in CDI 1.1

Weld 警告我

servletWeldServlet [WARN] WELD-ENV-001004:同时找到 WEB-INF/beans.xml 和 WEB-INF/classes/META-INF/beans.xml。同时使用这两个位置并不便携。 Weld 将使用 jndi:/localhost/webproject/WEB-INF/beans.xml。

一切都可以正常工作。

所以,我删除了所有 beans.xml,因为我不应该在 CDI 1.2 中需要它们,但之后就没有任何效果了:WELD-001408: Unsatisfied dependencies...

我错过了什么?这是网页碎片问题吗?

【问题讨论】:

    标签: cdi jboss-weld web-fragment


    【解决方案1】:

    注入点 [BackedAnnotatedMethod] 或类似的不满足的依赖关系

    显示解析失败的注入点,检查注入字段的类型是否被注释。

    即:

    @Inject
    private SomeService injectedService;
    

    然后CDI1.2规定如果没有定义beans.xml,它只会扫描带注释的bean。

    因此

    @SessionScoped or 
    {@ApplicationScoped, @Dependent, @RequestScoped, @ConversationScoped}
    public class SomeService{}
    

    【讨论】:

    • 是的,我知道。我从 javax.enterprise.* 导入它们。就像 CDI 忽略了没有 beans.xml 的 web 片段 JARS。
    猜你喜欢
    • 2020-04-07
    • 2012-05-19
    • 2013-02-15
    • 2011-01-14
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    相关资源
    最近更新 更多