I have troubles getting a javax.servlet.ServletConfig into a class annotated with org.springframework.context.annotation.Configuration.
My team decided that we should use spring for dependency injection and I'm trying to use it to migrate one of our simple Rest services.
My constraints are:
- JAX-RS: We have several REST Services implemented JAX-RS and we don't really want to change that.
- Not bound to a specific implementation of JAX-RS (Jersey & RESTEasy work fine for us and we can change from one to the other without changing underlying code)
- Import as few dependencies as possible from spring: at the moment I import only org.springframework:spring-contextfrom the spring project.
- No API breakage: Deprecated is fine but the service should keep working during the transition, using our old way of doing things.
- A string parameter is defined in the service's web.xml. I need to get it, instantiate a Bean with it and inject the resulting bean at several place in the code.
- I don't want to mess with Spring Boot/MVC/... as the service already works and I just want the Dependency Injection part.
What I already have:
The code use javax.ws.rs.core.Application, with a class that look like that:
public class MyApplication extends Application {
  @Context
  private ServletConfig cfg;
  public DSApplication() {
  }
  @Override
  public Set<Class<?>> getClasses() {
      return new HashSet<>();
  }
  @Override
  public Set<Object> getSingletons() {
    Set<Object> set = new HashSet<>();
    String injectionStr = cfg.getInitParameter("injection");
    boolean injection = false;
    if (null != injectionStr && !injectionStr.isEmpty()) {
        injection = Boolean.valueOf(injectionStr);
    }
    if (injection) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
                DSServiceProducer.class,
                CContextBeanProvider.class
        );
        IDSService service = context.getBean(IDSService.class);
        set.add(service);
    } else {
        set.add(new DSService()); //Old way
    }
    return set;
  }
}
I need the servlet config in CContextBeanProvider, which look like:
@Configuration
public class CContextBeanProvider {
  private ServletConfig cfg; // How to get this here ?
  @Bean
  public CContextBean cContextBean() {
    String bean = cfg.getInitParameter("cpuContext");
    return new CContextBean(bean);
  }
}
CContextBean is a setting bean initialized from a string found in the web.xml of the service.
- Is it possible ?
- Do you have any idea how ?
- Would it be easier with CDI, knowing that we run on base Tomcat ? (I've already find this if I need to use tomcat with CDI)
 
    