Jersey 1 @Inject 迁移到 Jersey 2 停止工作

Jersey 1 @Inject migrated to Jersey 2 stopped working

我正在将我的系统从 Jersey 1 迁移到 Jersey 2。 在我的代码中,我有来自 com.sun.jersey.spi.inject.Inject@Inject 注释和来自 com.sun.jersey.spi.resource.Singleton@Singleton 注释。我已将它们更改为 javax.inject.Injectjavax.inject.Singleton.

自此更改后,我在注入任何用它注释的对象时遇到错误。我的错误是这个

javax.servlet.ServletException: A MultiException has 4 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=APIConnectorHandler,parent=BarcodeSearchClient,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1090224052)
2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=APIConnectorHandler,parent=RSearchClient,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1247004825)
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.search.barcode.BarcodeSearchClient errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on com.search.barcode.BarcodeSearchClient

    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:423)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    com.filter.MetricsEmittingFilter.doFilter(MetricsEmittingFilter.java:89)

APIConnectorHandlerRSearchClientBarcodeSearchClient 中都被注入了。我首先调用了在流程中命中 RSearchClient 的端点,然后调用了 BarcodeSearchClient,因为 RSearchClient 也显示为异常 #2。

我的web.xml看起来像这样

<?xml version="1.0"?>
<!--
When modifying this file, DO NOT FORGET to also modify workspace-web.xml
if necessary so that "brazil-build server" will work.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <context-param>
        <description>Spring Expression Language Support</description>
        <param-name>springJspExpressionSupport</param-name>
        <param-value>false</param-value>
    </context-param>
    <listener>
        <listener-class>com.abc.listener.ContextListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>JerseyServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>
                com.search, com.fasterxml.jackson.jaxrs.json
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JerseyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <filter-mapping>
        <filter-name>MetricsEmittingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

我确实发现了一些 Hk2 找不到豆子和东西的东西,但我无法理解的是它在 Jersey 1 中是如何工作的。Jersey 1 @Inject 在哪里得到豆子来自?

PS。这是一个超级遗留代码,我真的不知道为什么,但它有 spring 集成,但 none 的对象是作为 beans 创建的。它们只是在多个地方用 new 初始化。 Spring 仅用于初始化区域特定的 bean。

成功了。我必须在 web.xml 中创建活页夹 class 和应用程序 class 和 link。活页夹 class 需要绑定我通过 @Inject 注入的所有 classes 及其依赖项。

已关注 https://www.appsdeveloperblog.com/dependency-injection-hk2-jersey-jax-rs/