title: Spring Boot with Apache CXF and CDI
body: <p>When using <a href="http://cxf.apache.org/docs/springboot.html#SpringBoot-SpringBootCXFJAX-RSStarter" rel="nofollow noreferrer">Apache's CXF JAX-RS Spring Boot starter</a> with the <a href="http://cxf.apache.org/docs/using-cxf-and-cdi-11-jsr-346.html" rel="nofollow noreferrer">CXF CDI dependency</a> (<code>cxf-integration-cdi</code>), Spring fails trying to do the autowiring because it only supports <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-standard-annotations" rel="nofollow noreferrer">JSR 330</a> and <a href="https://jira.spring.io/browse/SPR-13132" rel="nofollow noreferrer">not CDI</a>. Is there a way to get CDI to work with Spring Boot?</p>
  
  <p>Code:</p>
  
  <pre><code>package com.ibm.test.webservices;
  
  import javax.enterprise.inject.Any;
  import javax.enterprise.inject.Default;
  import javax.enterprise.inject.Instance;
  import javax.inject.Inject;
  import javax.inject.Named;
  import javax.ws.rs.ApplicationPath;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Application;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  @ApplicationPath("/")
  @Path("/")
  public class TestWebServices extends Application {
      public static void main(String[] args) {
          SpringApplication.run(
              TestWebServices.class,
              "--cxf.path=/",
              "--cxf.jaxrs.classes-scan=true",
              "--cxf.jaxrs.classes-scan-packages=" +
                  TestWebServices.class.getPackage().getName()
          );
      }
  
      @Inject
      @Any
      private Instance&lt;InvokerInterface&gt; impl;
  
      @GET
      @Produces("text/plain")
      @Path("/")
      public String helloWorld() {
          return impl.get().invoke();
      }
  
      public interface InvokerInterface {
          String invoke();
      }
  
      @Named
      @Default
      public static class Implementation1 implements InvokerInterface {
          public String invoke() {
              return "Hello World 1\n";
          }
      }
  
      @Named
      public static class Implementation2 implements InvokerInterface {
          public String invoke() {
              return "Hello World 2\n";
          }
      }
  }
  </code></pre>
  
  <p>pom.xml:</p>
  
  <pre><code>&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/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.ibm.test&lt;/groupId&gt;
      &lt;artifactId&gt;test-spring-boot-with-cdi&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;Test Spring Boot with Apache CXF and CDI&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;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.6.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
      &lt;dependencies&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.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;org.apache.cxf&lt;/groupId&gt;
              &lt;artifactId&gt;cxf-integration-cdi&lt;/artifactId&gt;
              &lt;version&gt;3.1.11&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;!-- Required for a standalone JAR: --&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>Exception:</p>
  
  <pre><code>2017-07-28 16:32:59.527 ERROR 9630 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
  
  ***************************
  APPLICATION FAILED TO START
  ***************************
  
  Description:
  
  Field impl in com.ibm.test.webservices.TestWebServices required a bean of type 'javax.enterprise.inject.Instance' that could not be found.
  
  
  Action:
  
  Consider defining a bean of type 'javax.enterprise.inject.Instance' in your configuration.
  
  [WARNING] 
  java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
      at java.lang.reflect.Method.invoke(Method.java:508)
      at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527)
      at java.lang.Thread.run(Thread.java:785)
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testWebServices': Unsatisfied dependency expressed through field 'impl'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.enterprise.inject.Instance&lt;com.ibm.test.webservices.TestWebServices$InvokerInterface&gt;' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject(), @javax.enterprise.inject.Any()}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
      at com.ibm.test.webservices.TestWebServices.main(TestWebServices.java:22)
      ... 6 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.enterprise.inject.Instance&lt;com.ibm.test.webservices.TestWebServices$InvokerInterface&gt;' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject(), @javax.enterprise.inject.Any()}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
      ... 25 more
  </code></pre>