title: Spring Boot JAX-RS/CXF dependency injection works in JAR but not WAR
body: <p>I'm wrapping an existing vanilla JAX-RS application JAR with a Spring Boot application using <a href="http://cxf.apache.org/docs/springboot.html#SpringBoot-AdditionalConfiguration.1" rel="noreferrer">cxf.jaxrs.classes-scan and cxf.jaxrs.classes-scan-packages</a>. When I run as a JAR or with <code>maven spring-boot:run</code>, dependency injection works fine. When I run as a WAR (on WebSphere Liberty 17.0.0.2), the <code>@Inject</code>-able fields are <code>null</code> during REST requests.</p>
  
  <p>Here's the <code>SpringBootApplication</code>:</p>
  
  <pre><code>@SpringBootApplication(scanBasePackages = { "com.test" })
  public class CustomerServiceApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
      SpringApplication.run(CustomerServiceApplication.class, args);
    }
  
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(CustomerServiceApplication.class);
    }
  }
  </code></pre>
  
  <p>Here's <code>src/main/resources/application.properties</code>:</p>
  
  <pre><code>cxf.path=/
  cxf.jaxrs.classes-scan=true
  cxf.jaxrs.classes-scan-packages=com.test,com.fasterxml.jackson.jaxrs.json
  </code></pre>
  
  <p>Here's the Maven <code>pom.xml</code> (the vanilla JAX-RS application JAR is customerservice-java which is in the local repository):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;customerservice-springboot&lt;/groupId&gt;
      &lt;artifactId&gt;customerservice-springboot&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;2.0.0-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;Customer Service :: Spring Boot&lt;/name&gt;
  
      &lt;!-- We need to use cxf-spring-boot-starter-jaxrs 3.2.0 because of https://issues.apache.org/jira/browse/CXF-7237 
          At the time of writing this code, the latest available version in Maven central 
          is 3.1.7 so we need to use the Apache snapshot repository. --&gt;
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;apache.snapshots&lt;/id&gt;
              &lt;name&gt;Apache Development Snapshot Repository&lt;/name&gt;
              &lt;url&gt;https://repository.apache.org/content/repositories/snapshots/&lt;/url&gt;
              &lt;releases&gt;
                  &lt;enabled&gt;false&lt;/enabled&gt;
              &lt;/releases&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;properties&gt;
          &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.4.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.cxf&lt;/groupId&gt;
              &lt;artifactId&gt;cxf-spring-boot-starter-jaxrs&lt;/artifactId&gt;
              &lt;version&gt;3.2.0-SNAPSHOT&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.json&lt;/groupId&gt;
              &lt;artifactId&gt;javax.json-api&lt;/artifactId&gt;
              &lt;version&gt;1.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.inject&lt;/groupId&gt;
              &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
              &lt;version&gt;1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;customerservice-java&lt;/groupId&gt;
              &lt;artifactId&gt;customerservice-java&lt;/artifactId&gt;
              &lt;version&gt;2.0.0-SNAPSHOT&lt;/version&gt;
              &lt;classifier&gt;jar&lt;/classifier&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.jaxrs&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-jaxrs-json-provider&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish&lt;/groupId&gt;
              &lt;artifactId&gt;javax.json&lt;/artifactId&gt;
              &lt;version&gt;1.0.4&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;repackage&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Here's the web service in the JAR project:</p>
  
  <pre><code>package com.test;
  import javax.inject.Inject;
  import javax.ws.rs.CookieParam;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Response;
  
  @Path("/")
  public class CustomerServiceRest {
  
    @Inject
    CustomerService customerService;
  
    @GET
    @Path("/byid/{custid}")
    @Produces("text/plain")
    public Response getCustomer(@PathParam("custid") String customerid, @CookieParam("token") String jwtToken) {
        return Response.ok(customerService.getCustomerId(customerid)).build();
    }
  }
  </code></pre>
  
  <p>Here's the bean:</p>
  
  <pre><code>package com.test;
  import javax.inject.Named;
  @Named
  public class CustomerService {
    public String getCustomerById(String username) {
      // ... implementation ..
      return customerDoc.toJson();
    }
  }
  </code></pre>
  
  <p>Here's the WAR logging output:</p>
  
  <pre><code>  .   ____          _            __ _ _
   /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
  ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
   \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
    '  |____| .__|_| |_|_| |_\__, | / / / /
   =========|_|==============|___/=/_/_/_/
   :: Spring Boot ::        (v1.5.4.RELEASE)
  INFO  c.a.s.CustomerServiceApplication - Starting CustomerServiceApplication on 23fb5f5646c3 with PID 20 (/opt/ibm/wlp/usr/servers/defaultServer/apps/expanded/customerservice-springboot-2.0.0-SNAPSHOT.war/WEB-INF/classes started by root in /opt/ibm/wlp/output/defaultServer)
  INFO  c.a.s.CustomerServiceApplication - No active profile set, falling back to default profiles: default
  INFO  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67e7ebcd: startup date [Wed Jul 19 19:36:12 UTC 2017]; root of context hierarchy
  INFO  o.s.b.f.x.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]
  INFO  o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
  INFO  c.i.w.w.webapp - SRVE0292I: Servlet Message - [customerservice-springboot-2.0.0-SNAPSHOT]:.Initializing Spring embedded WebApplicationContext
  INFO  o.s.w.c.ContextLoader - Root WebApplicationContext: initialization completed in 3654 ms
  INFO  o.s.b.w.s.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]
  INFO  o.s.b.w.s.ServletRegistrationBean - Mapping servlet: 'CXFServlet' to [/*]
  INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'errorPageFilter' to: [/*]
  INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
  INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
  INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
  INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67e7ebcd: startup date [Wed Jul 19 19:36:12 UTC 2017]; root of context hierarchy
  INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
  INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity&lt;java.util.Map&lt;java.lang.String, java.lang.Object&gt;&gt; org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
  INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  INFO  o.a.c.e.ServerImpl - Setting the server's publish address to be /
  INFO  o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
  INFO  c.a.s.CustomerServiceApplication - Started CustomerServiceApplication in 14.955 seconds (JVM running for 271.749)
  INFO  o.a.c.e.S.e.o.i.l.E.i.w.j.2.0.c.0.17.cl170220170523-1818(id=171)] - Setting the server's publish address to be /
  </code></pre>
  
  <p>If I enable <code>logging.level.org.springframework.beans.factory.support=TRACE</code> in <code>application.properties</code>, I see the dependency injection working during application startup:</p>
  
  <pre><code>DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'customerService'
  DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'customerService'
  DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'customerService' to allow for resolving potential circular references
  DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'customerService' [...]
  DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'com.test.CustomerServiceRest'
  DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'customerService'
  DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'com.test.CustomerServiceRest'
  </code></pre>
  
  <p>However, when I make REST requests, I can see that a new instance of <code>CustomerServiceRest</code> is created on each request (<code>System.out.println</code> in the constructor) and the <code>@Inject</code>-able dependencies are <code>null</code> (resuting in a <code>NullPointerException</code>). So I thought maybe adding <code>@Singleton</code> to <code>CustomerServiceRest</code> would work, but a new object is still created on each request.</p>
  
  <p>Does anyone know how to either use a single web service bean or to ensure that Spring injects all the dependencies? The vanilla JAX-RS application JAR can't itself take on any Spring dependencies.</p>