title: jersey-spring3 instantiating Spring-managed bean (null!)
body: <p>I first want to point out that this is by and large the biggest problem in terms of time wasted that I have ever dealt with in my career.  (Over two days straight now with essentially 0 progress.)  Every single "work-around" or "solution" I have attempted hasn't worked, so I am blocked and pretty desperate for some assistance.</p>
  
  <p>The problem in a nutshell is that Jersey/HK2 seems to always instantiate my Spring-managed beans AFTER they have already been instantiated by Spring, which tells me that <a href="https://jersey.java.net/documentation/latest/spring.html">jersey-spring3</a> is not doing its job, or at least not with my current setup (or any of the ~50 permutations of setups I have tried thus far.)</p>
  
  <p><strong>Note that when I use an empty constructor, those resource fields are null at run-time.</strong></p>
  
  <p>I do not understand why my current setup doesn't work as I am essentially <a href="https://github.com/ivarconr/jersey2-spring3-webapp/">copying this online example</a></p>
  
  <p>Any help is more than appreciated!!</p>
  
  <h1>Configuration</h1>
  
  <p><strong>- - - - - pom.xml - - - - -</strong></p>
  
  <pre><code>&lt;!-- ... --&gt;
  
  &lt;dependencies&gt;
  
      &lt;!-- Spring Dependencies --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-instrument&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-instrument-tomcat&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-ldap&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-parent&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;classifier&gt;tests&lt;/classifier&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;classifier&gt;tests&lt;/classifier&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-acl&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-support&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-dao&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- / Spring Dependencies --&gt;
  
      &lt;!-- API dependencies --&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
          &lt;version&gt;${jersey.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
          &lt;version&gt;${jersey.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-media-json-processing&lt;/artifactId&gt;
          &lt;version&gt;${jersey.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.test-framework.providers&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-test-framework-provider-grizzly2&lt;/artifactId&gt;
          &lt;version&gt;2.4&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-spring3&lt;/artifactId&gt;
          &lt;version&gt;${jersey.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
          &lt;artifactId&gt;gson&lt;/artifactId&gt;
          &lt;version&gt;${gson.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- / API dependencies --&gt;
  
      &lt;!-- ... --&gt;
  
  &lt;/dependencies&gt;
  
  &lt;!-- ... --&gt;
  
  &lt;properties&gt;
  
      &lt;!-- ... --&gt;
  
      &lt;spring.version&gt;3.0.5.RELEASE&lt;/spring.version&gt;
  
      &lt;jersey.version&gt;2.4.1&lt;/jersey.version&gt;
  
      &lt;gson.version&gt;2.2.4&lt;/gson.version&gt;
  
      &lt;!-- ... --&gt;
  
  &lt;/properties&gt;
  
  &lt;!-- ... --&gt;
  </code></pre>
  
  <p><strong>- - - - - web.xml - - - - -</strong></p>
  
  <pre><code>&lt;web-app&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:/beans.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- ... --&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
              &lt;param-value&gt;fubar.rest.FubarJerseyApplication&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/api/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;!-- ... --&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>- - - - - beans.xml (Context Configuration) - - - - -</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  &lt;!-- ... --&gt;
  
  &lt;!-- beans-outbound-api has configuration for spring-jersey3 to work properly --&gt;
  &lt;import resource="beans-api.xml" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>- - - - - beans-api.xml - - - - -</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;!-- Services --&gt;
  
      &lt;bean id="locationServiceV1" class="fubar.rest.v1.services.location.impl.LocationServiceV1" /&gt;
      &lt;bean id="locationServiceV2" class="fubar.rest.v2.services.location.impl.LocationServiceV2" /&gt;
  
      &lt;bean id="viewServiceV1" class="fubar.rest.v1.services.view.impl.ViewServiceV1" /&gt;
      &lt;bean id="viewServiceV2" class="fubar.rest.v2.services.view.impl.ViewServiceV2" /&gt;
  
      &lt;!-- Resources --&gt;
  
      &lt;bean class="fubar.rest.resources.location.impl.LocationResource"&gt;
          &lt;constructor-arg index="0" ref="locationServiceV1" /&gt;
          &lt;constructor-arg index="1" ref="locationServiceV2" /&gt;
      &lt;/bean&gt;
  
      &lt;bean class="fubar.rest.resources.view.impl.ViewResource"&gt;
          &lt;constructor-arg index="0" ref="viewServiceV1" /&gt;
          &lt;constructor-arg index="1" ref="viewServiceV2" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <h1>Code</h1>
  
  <p><strong>- - - - - Resource (JAX-RS) - - - - -</strong></p>
  
  <pre><code>@Path(RESTLocationResourceV1.PathFields.PATH_ROOT)
  @Produces({V1_JSON, APPLICATION_JSON})
  public class LocationResource
      extends ResourceBase&lt;LocationResource, ILocationServiceV1, ILocationServiceV2&gt; {
  
    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
    }
  }
  </code></pre>
  
  <p><strong>- - - - - Service (Spring Bean) - - - - -</strong></p>
  
  <pre><code>public class LocationServiceV1 extends ServiceBaseV1&lt;LocationBean, LocationV1, LocationListV1&gt;
      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
    }
  }
  </code></pre>
  
  <p><em>(Essentially the same for version 2)</em></p>
  
  <p><strong>- - - - - Application (Jersey) - - - - -</strong></p>
  
  <pre><code>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()));
    }
  }
  </code></pre>
  
  <h1>Invocation (Client)</h1>
  
  <pre><code>curl http://my-ip-address/fubar/api/location/list
  </code></pre>
  
  <p><em>(500 Internal Server Error)</em></p>
  
  <h1>Error (Server)</h1>
  
  <pre><code>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)
  </code></pre>
  
  <h1>API Log</h1>
  
  <pre><code>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
  </code></pre>
  
  <hr>
  
  <p>Update -- found this:</p>
  
  <h1>Catalina Log</h1>
  
  <pre><code>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
  </code></pre>
  
  <p>... so the ApplicationContext isn't found in <a href="https://github.com/jersey/jersey/blob/master/ext/spring3/src/main/java/org/glassfish/jersey/server/spring/SpringComponentProvider.java#L82">SpringComponentProvider#initialize</a>.</p>