Jersey-spring3 实例化 Spring 管理的 Bean(null!)

11

首先我要指出,这绝对是我职业生涯中浪费时间最多的问题。(已经连续两天了,几乎没有任何进展。)每一个所谓的“解决方法”和“应对方案”都无法奏效。因此,我现在被卡住,非常渴望得到帮助。

问题简而言之是:Jersey/HK2 似乎总是在 Spring 已经实例化了我的 bean 后才实例化它们,这说明 jersey-spring3 并未完成其工作,或者至少在我的当前设置下没有(或者我迄今尝试过的 ~50 种不同设置中没有一种能够工作。)

请注意,当我使用空构造函数时,那些资源字段在运行时为 null。

我不明白为什么我的当前设置不起作用,因为我本来就是在仿照这个在线示例去操作。

非常感谢你们能提供帮助!

配置

- - - - - pom.xml - - - - -

<!-- ... -->

<dependencies>

    <!-- Spring Dependencies -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument-tomcat</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-parent</artifactId>
        <version>${spring.version}</version>
        <classifier>tests</classifier>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.version}</version>
        <classifier>tests</classifier>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-acl</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-support</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-dao</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- / Spring Dependencies -->

    <!-- API dependencies -->

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-processing</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
        <version>2.4</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
    </dependency>

    <!-- / API dependencies -->

    <!-- ... -->

</dependencies>

<!-- ... -->

<properties>

    <!-- ... -->

    <spring.version>3.0.5.RELEASE</spring.version>

    <jersey.version>2.4.1</jersey.version>

    <gson.version>2.2.4</gson.version>

    <!-- ... -->

</properties>

<!-- ... -->

- - - - - web.xml - - - - -

<web-app>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/beans.xml</param-value>
    </context-param>

    <!-- ... -->

    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>fubar.rest.FubarJerseyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <!-- ... -->

</web-app>

- - - - - beans.xml(上下文配置)- - - - -

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- ... -->

<!-- beans-outbound-api has configuration for spring-jersey3 to work properly -->
<import resource="beans-api.xml" />

</beans>

- - - - - beans-api.xml - - - - -

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Services -->

    <bean id="locationServiceV1" class="fubar.rest.v1.services.location.impl.LocationServiceV1" />
    <bean id="locationServiceV2" class="fubar.rest.v2.services.location.impl.LocationServiceV2" />

    <bean id="viewServiceV1" class="fubar.rest.v1.services.view.impl.ViewServiceV1" />
    <bean id="viewServiceV2" class="fubar.rest.v2.services.view.impl.ViewServiceV2" />

    <!-- Resources -->

    <bean class="fubar.rest.resources.location.impl.LocationResource">
        <constructor-arg index="0" ref="locationServiceV1" />
        <constructor-arg index="1" ref="locationServiceV2" />
    </bean>

    <bean class="fubar.rest.resources.view.impl.ViewResource">
        <constructor-arg index="0" ref="viewServiceV1" />
        <constructor-arg index="1" ref="viewServiceV2" />
    </bean>

</beans>

代码

- - - - - 资源 (JAX-RS) - - - - -

@Path(RESTLocationResourceV1.PathFields.PATH_ROOT)
@Produces({V1_JSON, APPLICATION_JSON})
public class LocationResource
    extends ResourceBase<LocationResource, ILocationServiceV1, ILocationServiceV2> {

  private static final Logger logger = Logger.getLogger(LocationResource.class);

  @Inject
  public LocationResource(final LocationServiceV1 v1Loc, final LocationServiceV2 v2Loc) {
    super(v1Loc, v2Loc);
    logger.info(format(Messages.INF_INSTANTIATED, "LocationResource"));
  }

  @GET
  @Path(PathFields.SUBPATH_LIST)
  public LocationListV1 listV1(@HeaderParam(HEADER_API_KEY) String apiKey)
      throws ApplicationException {
    // Implementation
  }

  @GET
  @Path(PathFields.SUBPATH_SEARCH)
  public LocationListV1 searchV1(@HeaderParam(HEADER_API_KEY) String apiKey,
      @QueryParam(QueryFields.QUERY) String likeText) throws ApplicationException {
    // Implementation
  }
}

- - - - - 服务 (Spring Bean) - - - - -

public class LocationServiceV1 extends ServiceBaseV1<LocationBean, LocationV1, LocationListV1>
    implements
      ILocationServiceV1 {

  @Autowired
  private LocationDao daoLoc;

  public LocationServiceV1() {
    super(new LocationBeanToJsonTranslatorV1());
  }

  @Override
  public LocationListV1 listV1() throws ApplicationException {
    // Implementation
  }

  @Override
  public LocationListV1 searchV1(String likeText) throws ApplicationException {
    // Implementation
  }
}

(版本2基本相同)

- - - - - 应用程序(Jersey)- - - - -

public class FubarJerseyApplication extends ResourceConfig {

  private static final class Messages {
    static final String INF_STARTING_APPLICATION = "Starting %s!";
  }

  private static final Logger logger = Logger.getLogger(FubarJerseyApplication.class);

  public FubarJerseyApplication() {
    packages("fubar.rest");
    logger.info(format(Messages.INF_STARTING_APPLICATION, this.getClass().getName()));
  }
}

调用(客户端)

curl http://my-ip-address/fubar/api/location/list

(500 服务器内部错误)

错误(服务器)

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object
available for injection at Injectee(requiredType=LocationServiceV1,parent=
LocationResource,qualifiers={}),position=0,optional=false,self=false,
unqualified=null,344016971)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:208)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:225)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:329)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456)
at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:158)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2350)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:612)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:597)
at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:103)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:128)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:131)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:110)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:65)
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:250)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:983)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:361)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at fubar.server.springframework.SessionFilter.doFilter(SessionFilter.java:44)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at fubar.server.springframework.loader.ContextLoaderHttpInterceptor$LoaderState.filter(ContextLoaderHttpInterceptor.java:75)
at fubar.server.springframework.loader.ContextLoaderHttpInterceptor$StartedState.filter(ContextLoaderHttpInterceptor.java:120)
at fubar.server.springframework.loader.ContextLoaderHttpInterceptor.doFilter(ContextLoaderHttpInterceptor.java:62)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:311)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
at java.lang.Thread.run(Thread.java:662)

API日志

Dec 10, 2013 13:36:28 INFO  [main] fubar.rest.FubarJerseyApplication
     - Starting fubar.rest.FubarJerseyApplication!
Dec 10, 2013 13:38:06 INFO  [pool-1-thread-1] resources.location.impl.LocationResource
     - LocationResource has been instantiated
Dec 10, 2013 13:38:06 INFO  [pool-1-thread-1] resources.view.impl.ViewResource
     - ViewResource has been instantiated

更新 - 发现以下内容:

Catalina日志

Dec 10, 2013 1:36:42 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.4.1 2013-11-08 12:08:47...
Dec 10, 2013 1:36:43 PM org.glassfish.jersey.server.spring.SpringComponentProvider initialize
SEVERE: Spring context lookup failed, skipping spring component provider initialization.
Dec 10, 2013 1:38:00 PM com.sun.xml.bind.v2.runtime.reflect.opt.Injector inject

因此,在SpringComponentProvider#initialize中找不到ApplicationContext。


欢迎加入俱乐部!:( 经过初步的困难后,我考虑放弃Jersey 2.7,转而选择Apache CXF或Spring MVC(不幸的是它不是JAX-RS...)。我想在此期间我们应该坚持使用Jersey 1.x吗? - Hendy Irawan
这篇文章很老,但是从我听到的消息中得知,jersey-spring3 的最新更新已经解决了这个问题。 - Ryan
3个回答

10
什么会先加载?Spring还是Jersey?可能是因为当SpringComponentProvider调用WebApplicationContextUtils.getWebApplicationContext(sc);时,您的Spring上下文尚未初始化。尝试使用Spring的ContextLoaderListener,以便Spring在应用程序部署后立即进行初始化。
我遇到了与jersey-spring3库相同的问题。它无法找到我的Spring ApplicationContext(看起来这就是您卡住的地方),并且注入具有通用参数作为参数的setter时会出错。
如果您解决了应用程序上下文问题,我认为您拥有的东西也不会起作用。您在XML中定义了ViewResource和LocationResource bean。据我所知,只有在资源类带有@Component注释时,Jersey才会从Spring获取资源实例。请查看org.glassfish.jersey.server.spring.SpringComponentProvider,特别是component.isAnnotationPresent(Component.class)
// detect JAX-RS classes that are also Spring @Components.
// register these with HK2 ServiceLocator to manage their lifecycle using Spring.
@Override
public boolean bind(Class<?> component, Set<Class<?>> providerContracts) {

    if (ctx == null) {
        return false;
    }

    if(component.isAnnotationPresent(Component.class)) {
        DynamicConfiguration c = Injections.getConfiguration(locator);
        String[] beanNames = ctx.getBeanNamesForType(component);
        if(beanNames == null || beanNames.length != 1) {
            LOGGER.severe(LocalizationMessages.NONE_OR_MULTIPLE_BEANS_AVAILABLE(component));
            return false;
        }
        String beanName = beanNames[0];

        ServiceBindingBuilder bb = Injections.newFactoryBinder(new SpringComponentProvider.SpringManagedBeanFactory(ctx, locator, beanName));
        bb.to(component);
        Injections.addBinding(bb, c);
        c.commit();

        LOGGER.config(LocalizationMessages.BEAN_REGISTERED(beanName));
        return true;
    }
    return false;
}

除此之外,我们还希望将所有的JAX-RS注释移动到接口中。但是每当我尝试时,都会出现“找不到适合com.foo.ResourceInterface的构造函数”的错误。

最终,我通过不使用jersey-spring3并自己编写Jersey到Spring连接器来解决了所有问题。以下是我的做法:

  1. 将所有资源配置为常规的Spring bean。如果您愿意,可以使用XML。
  2. 在我的应用程序中,我添加了HK2容器的绑定,以便在需要资源实例的情况下使用工厂。我的工厂类只需返回Spring管理的资源实例即可。
  3. 在工厂返回Spring管理的bean之前,我使用Jersey/HK2 ServiceLocator注入Jersey提供的内容。例如,任何带有@Context注释的内容。

我的javax.ws.rs.Application如下所示:

public class RestConfig extends ResourceConfig {
private static final Log log = LogFactory.getLog(RestConfig.class);


@Inject
public RestConfig(ServiceLocator locator) {
    super();
    // specific to my app. get your spring beans however you like
    Collection<Object> beans = BeanLocator.beansByAnnotation(RestResource.class);

    DynamicConfiguration c = Injections.getConfiguration(locator);


    for (Object bean : beans)
    {
                    // tell jersey to use a factory for any interface that the bean implements.  since your resources don't implement interfaces, 
                    // you'll want to do something a bit different here.
        for (Class<?> currentInterface : bean.getClass().getInterfaces())
        {
            if (log.isTraceEnabled())
                log.trace("binding " + currentInterface.getSimpleName() + " to Spring managed bean");

            ServiceBindingBuilder<Object> bb = Injections.newFactoryBinder(new StaticLookupFactory(locator, bean));
            bb.to(currentInterface);
                    Injections.addBinding(bb, c);
        }
    }

            // commit the changes to the HK2 container (don't skip this step!)
            c.commit();

    property("jersey.config.disableMoxyJson.server", true);
    packages("com.foo.web.rest");

    register(MoxyXmlFeature.class);
}


// a "factory" where the provide() method returns the spring managed bean
    // that was passed to the constructor.
private static class StaticLookupFactory implements Factory<Object> {

    private ServiceLocator locator;
    private Object bean;

    StaticLookupFactory(ServiceLocator locator, Object bean)
    {
        this.locator = locator;
        this.bean = bean;
    }

    @Override
    public Object provide() {
                    // inject this annotated with @Context, @Inject, etc
        locator.inject(bean);
        return bean;
    }

    @Override
    public void dispose(Object instance) {
    }

}
}

BeanLocator是我编写的实用类,用于在没有自动装配的情况下使用静态方法轻松获取bean实例。例如,在Spring管理的bean之外工作时。这里没有太多的操作:

public static Collection<Object> beansByAnnotation(Class<? extends Annotation> annotation)
{
    return applicationContext.getBeansWithAnnotation(annotation).values();
}

RestResource也是我们应用程序特定的。它是一种自定义的构造,类似于@Component、@Service等:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface RestResource {
    String value() default "";
}

请注意,Jersey允许您注册自定义实现的org.glassfish.jersey.server.spring.ComponentProvider来管理资源的生命周期。我尝试过了,但无论我做什么都无法使其识别我的实现。
另一个需要注意的地方是,locator.inject(bean)调用会激活Jersey依赖注入机制,并处理任何带有@Inject标记的内容。在类中使用@Autowired或通过XML配置您的bean以避免Spring和Jersey都尝试解析带有@Inject注释的值。

感谢您的回复 - 我稍后会研究一下您所做的事情。只是想评论一下“一个无关的问题是,我们还想将所有的JAX-RS注释移动到接口中。每当我尝试时,我都会得到“找不到适合com.foo.ResourceInterface的构造函数”。 “-我能够通过手动注册所有提供程序和资源来解决这个问题。当您通过包扫描注册资源时,就会发生此错误。(绝对是Jersey的错误...) - Ryan
非常感谢!这个解决了我的问题。我把ContextLoaderListener放在web.xml中作为第一个监听器,这显然已经为我解决了问题。但是,当我将web.xml中的servlet-api版本从2.5更新到3.0时,又遇到了同样的问题。然后,我还重新排列了web.xml中的servlet,以便一个Spring servlet(WSSpringServlet)位于第一位置。否则,一些其他库会导致SpringComponentProvider首先被初始化。 - avidD

3
我们有一个定制的异步ContextLoader,因此临时解决方案需要在Jersey-Spring3源代码中放置一个总体性的hack,在自定义组件提供程序初始化之前等待应用程序初始化。
附言:对于任何需要执行此类操作的可怜灵魂,请确保META-INF/settings包含SpringComponentProvider配置。

(2014-04-18) 为@Scott作详细解释

请注意,这是一个可怕的黑客行为,只有在所有其他尝试失败时才会尝试这样的事情,就像我的情况一样。此外,在尝试任何类似操作之前,请咨询Jersey邮件组关于您的问题。

话虽如此...这就是我解决问题的方法:

  • 直接复制spring-jersey3的源代码到我的应用程序/服务器中,并根据许可证修改每个文件的头部标签;

  • 创建以下类 -

===>

  /**
   * Hack class for RN-8979.
   *
   * @author ryan
   *
   */
  public class ContextLoadWaiter {

    private static final Logger logger = Logger.getLogger(ContextLoadWaiter.class);

    public void doWait() {

      try {
        while (ContextLoaderHttpInterceptor.isNotStarted()) {
          logger.info("Waiting for ContextLoader to start...");
          Thread.sleep(1000);
        }
      } catch (InterruptedException e) {
        logger.error("SpringComponentProvider was interrupted!");
      }
    }
  }

请注意,这是针对*我们*的代码库的特定情况,因为ContextLoaderHttpInterceptor是一个http servlet,其中isNotStarted返回true,如果我们自定义的ContextLoader(恰好是异步的)尚未加载。
某个时候,某人为了某种原因放置了自定义的异步ContextLoader,以允许UI在服务器启动时显示“加载”页面。(可能不是添加此UI“功能”的正确方式,但代码已经存在,UI依赖于它,所以我必须处理它...)

由于这部分不直接适用于您,关键是通过 SpringComponentProvider(来自 这里)进行调试,并查看 ClassPathXmlApplicationContext 的值。 如果它为 null,就像我们的情况一样,那么您需要弄清楚为什么它为 null 并等待您使用的任何 ContextLoader 在初始化此组件之前加载。

  • 将这个 hacky 行放在 SpringComponentProvider 中--

==>

  ...

  private final ContextLoadWaiter waiter = new ContextLoadWaiter();

  ...

  @Override
  public void initialize(ServiceLocator locator) {

    waiter.doWait(); // Wait on our asynchronous context loader.

    this.locator = locator;

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Context lookup started");
    }

    ...
  • 创建了这个文件:META-INF/services/org.glassfish.jersey.server.spi.ComponentProvider,其中包含SpringComponentProvider的完全限定类路径,例如com.company.server.nbi.rest.internal.jspring.SpringComponentProvider

  • 将自定义的Jersey-spring3包添加为应用程序中要扫描的包;请参见下面的内容...

/**
 * Application configuration.
 *
 * @author ryan
 *
 */
public class MyJerseyApplication extends ResourceConfig {

  private static final class Messages {
    static final String INF_STARTING_APPLICATION = "Starting %s!";
  }

  private static final Logger logger = Logger.getLogger(MyJerseyApplication.class);

  public MyJerseyApplication() {

    packages(
    /* Internal providers */
    "com.company.server.nbi.rest.providers",
    /* Internal filters */
    "com.company.server.nbi.rest.filters",
    /* Spring injection support */
    "com.company.server.nbi.rest.internal.jspring", // HERE!!!
    /* Json providers */
    "com.fasterxml.jackson.jaxrs.json",
    /* Jackson exception mappers */
    "com.fasterxml.jackson.jaxrs.base");

    /* Resources */
    register(ResourceA.class);
    register(ResourceB.class);
    register(ResourceC.class);

    /* Miscellaneous features */
    register(MultiPartFeature.class);
    register(LoggingFilter.class);

    logger.info(format(Messages.INF_STARTING_APPLICATION, this.getClass().getName()));
  }
}

这就是"它"。虽然不是一个值得骄傲的解决方案,但如果你像我一样处于绝望状态,尝试一下也无妨。


我修改了一个错误——你需要的文件名为META-INF/services/org.glassfish.jersey.server.spi.ComponentProvider。 - Ryan
我可以问一下吗,Jersey Spring 给你什么?能注入 Autowired Spring Beans 吗?难道没有其他合法的使用 Spring API 获取它们的方法吗? - Kalpesh Soni
@KalpeshSoni 关于“Jersey Spring”,是的...具备注入Autowired bean的能力。至于Jersey本身...我所在的公司在我开始参与该项目时使用的是一个古老版本的Spring,而Jersey似乎是避免兼容性问题的好选择。 - Ryan

1
这是理解问题关键的信息。它表明Spring未能正确初始化:
SEVERE: Spring context lookup failed, skipping spring component provider initialization.

(顺便提一下:由于Spring无法初始化,唯一尝试解决@Inject的JSR-330实现是HK2 - 这就是为什么您会看到其他问题的原因。)
无论如何,问题可能是您的容器没有扫描使所有jersey-spring3魔术发生的注释。
这种行为是Servlet 3.0规范(JSR-33,第1.6.2节)的一部分,因此您应该确保您的容器支持此规范。
在Tomcat的情况下 - 除非您运行Tomcat 7.0.29或更高版本,否则您实际上需要确保在web.xml中指定了Servlet版本。

http://tomcat.apache.org/tomcat-7.0-doc/changelog.html#Tomcat_7.0.29_(markt)

我最近遇到了这个问题,让我非常烦恼,但修复web.xml比从Ubuntu/Precise升级更容易!

希望这可以帮助你!


WIN:我也遇到了一个微妙的故障,接着是一个嘈杂的故障。你的帖子让我先关注微妙的故障 - 谢谢! - sea-rob
1
没关系,不幸的是看起来Jetty甚至不支持servlet 3.0扫描(如果你正在使用它)。Dropwizard看起来越来越好 :) - bly
Dropwizard看起来不错,谢谢你指出来。 - Ryan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接