首页 > 解决方案 > 在基于 Jersey 的应用程序中使用 Google Guice for DI 在 weblogic 的服务器启动时出现 WELD 异常

问题描述

我使用 Weblogic 12b 作为应用服务器。我的应用程序在我的项目中使用带有 Guice3 的 Jersey 2.5.1。我有一个从 org.glassfish.jersey.server.ResourceConfig 派生的名为 Application 的类。在服务器启动时,我收到如下错误:

Caused By: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ServiceLocator with qualifiers @Default
  at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public Application(ServiceLocator)
  at Application.<init>(Application.java:22)

        at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
        at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
        at org.jboss.weld.bootstrap.Validator.validateProducer(Validator.java:417)
        at org.jboss.weld.injection.producer.InjectionTargetService.validateProducer(InjectionTargetService.java:36)
        at org.jboss.weld.manager.InjectionTargetFactoryImpl.validate(InjectionTargetFactoryImpl.java:135)

似乎它正在用 WELD 代替 DI 的 google Guice。

我在业务层遇到了同样的问题,其中 EJB 类由 Java 类组成,它们是使用 @Inject 注入的。

我什至尝试将他的导入 @Inject 更改为谷歌注入,但异常已更改但未解决。

我尝试在 web-inf 中使用 beans.xml

@ApplicationPath("/")
public class Application extends ResourceConfig {

    @Inject
    public Application(final ServiceLocator serviceLocator) {
    }
}

标签: jersey-2.0weblogic12cjava-ee-7weldguice-3

解决方案


您需要有效地禁用 CDI。

您可以通过将 WEB-INF/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"
       bean-discovery-mode="none">
</beans>

注意bean-discovery-mode="none".

如果您的项目包含带有可能看起来 CDI bean 的类的其他 jar,那么您还需要向这些 jar 添加类似的 META-INF/beans.xml 文件。

但是,我怀疑这可能会导致其他不相关的问题。通常应用程序服务器喜欢控制类的生命周期,这包括与 JAX-RS 相关的那些。


推荐阅读