焊接找不到CDI生产商

时间:2017-03-14 10:29:08

标签: wildfly cdi weld

使用了WildFly 10.1。在模块中添加了包含带有接口EzRegistryService的jar文件的单个模块。

制片人代码:

package com.ejbtest;

import org.apache.log4j.Logger;

import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Produces;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Resources {
    private static final Logger log = Logger.getLogger(Resources.class);
    public static final String jndiName = "java:global/ejbtest.main/EzRegistryServiceBean!com.ejbtest.EzRegistryService";

    //also didn't work
    //@Produces
    //@EJB(lookup = jndiName)
    //private EzRegistryService ezRegistryService;

    // didn't work
    @Produces
    public EzRegistryService getEzRegistryService() {
        log.info("getEzRegistryService called");
        try {
            InitialContext ctx = new InitialContext();
            Object service = ctx.lookup(jndiName);
            log.info("Found: " + service);
            return (EzRegistryService) service;
        } catch (NamingException e) {
            log.error("Exception while getting EzRegistryService", e);
            return null;
        }
    }
}

Singleton bean:

package com.ejbtest;

import org.apache.log4j.Logger;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

@Singleton
@Startup
public class MyGateStartup {
    private static final Logger log = Logger.getLogger(MyGateStartup.class);

    //@EJB(mappedName = Resources.jndiName) //works fine
    @Inject
    private EzRegistryService service;

    @PostConstruct
    public void start() {
        log.info("MyGateStartup started");
        //...
    }

    @PreDestroy
    public void end() {
        //...
        log.info("MyGateStartup stopped");
    }

}

WEB-INF文件夹中的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.1"
       bean-discovery-mode="all">
</beans>

WildFly无法部署生成的war文件:

2017-03-14 13:15:05,237 MSC service thread 1-8 INFO  [org.jboss.weld.deployer] WFLYWELD0006: Starting Services for CDI deployment: ejbtest.gate-1.0-SNAPSHOT.war
2017-03-14 13:15:05,241 MSC service thread 1-8 INFO  [org.jboss.weld.deployer] WFLYWELD0009: Starting weld service for deployment ejbtest.gate-1.0-SNAPSHOT.war
2017-03-14 13:15:05,299 MSC service thread 1-6 ERROR [org.jboss.msc.service.fail] MSC000001: Failed to start service jboss.deployment.unit."ejbtest.gate-1.0-SNAPSHOT.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."ejbtest.gate-1.0-SNAPSHOT.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.6.Final.jar:1.2.6.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_121]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_121]
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EzRegistryService with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private com.ejbtest.MyGateStartup.service
  at com.ejbtest.MyGateStartup.service(MyGateStartup.java:0)

为什么CDI无法在Resources类中找到生产者?

2 个答案:

答案 0 :(得分:0)

在CDI规范中,它是written in section 2.5

未发现其bean类没有定义注释的bean的生产者方法(如生产者方法中所定义)....

您拥有@Produces注释方法的类必须是CDI bean。试着给它一个范围:)

答案 1 :(得分:0)

看起来CDI没有管理您的Resources课程。

一旦它只是一个工厂bean,你可以用@ApplicationScoped注释它:

@ApplicationScoped
public class Resources {
    ...
}