title: Spring boot and Jersey Filter with Dependency Injection
body: <p>Having a Spring boot with Jersey, I want to use some filters which internally use dependency injection.</p>
  
  <p>The problem is, that it seems the Jersey Server is not able to use the ApplicationContext of Spring, where we define that dependency. </p>
  
  <p>Here the JerseyConfiguration</p>
  
  <pre><code>@Configuration
  public class JerseyConfig extends ResourceConfig {
  
      private static final String CONTEXT_CONFIG = "contextConfig";
  
      private static final String RESOURCE_PACKAGE = "presentation.resource";
  
      public JerseyConfig() {
          ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
          applicationContext.refresh();
          property(CONTEXT_CONFIG, applicationContext);
          register(SpringLifecycleListener.class);
          register(MyFilter.class); //Filter I want to use 
          packages(RESOURCE_PACKAGE);
      }
  }
  </code></pre>
  
  <p>Here the Filter that has an <code>@Autowire</code></p>
  
  <pre><code>@Priority(40)
  @Primary
  @Provider
  @MyFilter
  class MyFilter(@Context var resourceInfo: ResourceInfo) extends ContainerRequestFilter with DefaultRuntime {
  
    implicit var finagleService: CommonFinagleService = _
  
    @Primary
    @Autowired(required = false)
    def setHttpFinagleService(@Autowired(required = false)
                              @Primary
                              _finagleService: CommonFinagleService): Unit = {
      finagleService = _finagleService
      finagleService.init(getProperties())
    }
  
    override def filter(context: ContainerRequestContext): Unit = {
      logger.debug(null, "filter running.....")
    }
  }
  </code></pre>
  
  <p>This is how it looks the <code>applicationContext.xml</code></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         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.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
  
      &lt;bean id="httpFinagleService" class="foo.bla.CommonFinagleService"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Finally the <code>Spring boot</code> main class</p>
  
  <pre><code>@Configuration
  @SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration.class,
          org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class,
          org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.class,
          org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class})
  public class SpringBootRestApplication extends SpringBootServletInitializer {
  
      public static void main(String[] args) {
  
          new SpringBootRestApplication().configure(new SpringApplicationBuilder(SpringBootRestApplication.class)).run(args);
      }
  </code></pre>
  
  <p>I run the main class, and the JerseyConfig class is invoked, and I´m able to register the filter. Unfortunately, once that the Service is running, when I run a request to one endpoint, the filter is invoked to the <code>setHttpFinagleService</code> method passing null dependency of <code>CommonFinagleService</code></p>
  
  <p>I'm also using <code>jersey-spring4</code> dependency </p>
  
  <pre><code>&lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-spring4&lt;/artifactId&gt;
              &lt;version&gt;2.27&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>And Spring boot and Spring is 2.1.0 and 5.1.8</p>
  
  <p>Any idea what can be wrong here</p>
  
  <p>With Grizzly + Jersey everything works just fine.</p>
  
  <p>Regards</p>