将 EJB 服务注入 Web 的不满足依赖项 (war)

Unsatisfied dependencies for injecting EJB service into web (war)

我的 beans.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<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="annotated">
</beans>

我的服务(后端 - EJB 类型)如下所示:

import javax.inject.Inject;
import javax.transaction.Transactional;

@Default
@Transactional(Transactional.TxType.REQUIRED)
public class UserServiceImpl implements UserService {

@Inject
private UserDao userDao;

@Inject
private UserMapper mapper;

@Named 带注释的 bean 使用此 (web):

import javax.inject.Inject;
import javax.inject.Named;
import org.omnifaces.cdi.ViewScoped;

@ViewScoped
@Named("indexMBean")
public class IndexMBean extends AbstractViewBean {

    @Inject
    private UserService userService;

构建成功,但部署给我这个异常:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UserService with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private hu.food.bean.IndexMBean.userService

如何设置 UserService 的默认注入?

日志说:

2018-11-12 11:28:25,706 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."kaJava-ear-0.0.1-SNAPSHOT.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."kaJava-ear-0.0.1-SNAPSHOT.ear".WeldStartService: Failed to start service at org.jboss.msc.service.ServiceControllerImpl$StartTask.execute(ServiceControllerImpl.java:1728) at org.jboss.msc.service.ServiceControllerImpl$ControllerTask.run(ServiceControllerImpl.java:1556) at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35) at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985) at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487) at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378) at java.lang.Thread.run(Thread.java:748) Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UserService with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private hu.food.bean.IndexMBean.userService at hu.food.bean.IndexMBean.userService(IndexMBean.java:0)

您将 bean 发现模式设置为带注释。所以只会检测到带有范围类型注释的 beans

bean-discovery-mode="annotated">

请为您的 bean 添加范围类型注释,如 @dependent

@Default
@Transactional(Transactional.TxType.REQUIRED)
@Dependent
public class UserServiceImpl implements UserService {

http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#bean_defining_annotations 说:

The set of bean defining annotations contains:

  • @ApplicationScoped, @SessionScoped, @ConversationScoped and
  • @RequestScoped annotations,

  • all other normal scope types,

  • @Interceptor and @Decorator annotations,

  • all stereotype annotations (i.e. annotations annotated with @Stereotype),

  • and the @Dependent scope annotation.

您正在 beans.xml 中使用 annotated 发现模式,这意味着只有具有所谓的 Bean 定义注释 的 beans 才会被发现并注册为 beans。查阅 CDI spec 以获取属于那里的所有注释;对于您的用例,您缺少 bean scope.

如果您从 UserServiceImpl 中删除 @Default 限定符(它没用,无论如何都会添加到那里;spec link)并向 bean 添加范围,它应该可以工作。 根据您的 bean 应该如何表现(从生命周期的角度来看),您可以将其设为 @ApplicationScoped@SessionScoped@RequestScoped@ConversationScoped@Dependent( , @Singleton).