Normegil
Normegil

Reputation: 157

JAX-RS, Spring & ServletConfig: How to access Servlet Config in Configurator

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:

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.

Upvotes: 0

Views: 557

Answers (1)

oracode
oracode

Reputation: 76

Could you please try to add all jersey CDI related jars to your applications ?

Upvotes: 0

Related Questions