0

我很难javax.servlet.ServletConfig进入一个用org.springframework.context.annotation.Configuration.

我的团队决定我们应该使用 spring 进行依赖注入,我正在尝试使用它来迁移我们的一项简单的 Rest 服务。

我的限制是:

  • JAX-RS:我们有几个实现了 JAX-RS 的 REST 服务,我们真的不想改变它。
  • 不受 JAX-RS 的特定实现的约束(Jersey 和 RESTEasy 对我们来说工作得很好,我们可以在不更改底层代码的情况下从一个更改到另一个)
  • 从 spring 中导入尽可能少的依赖项:目前我只org.springframework:spring-context从 spring 项目中导入。
  • 没有 API 损坏:弃用很好,但服务应该在过渡期间继续工作,使用我们旧的做事方式。
  • 字符串参数在服务的web.xml. 我需要得到它,用它实例化一个 Bean 并将生成的 bean 注入代码中的多个位置。
  • 我不想弄乱 Spring Boot/MVC/...,因为该服务已经工作了,我只想要依赖注入部分。

我已经拥有的:

代码使用javax.ws.rs.core.Application,其类如下所示:

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;
  }
}

我需要 servlet 配置CContextBeanProvider,如下所示:

@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 是从服务的 web.xml 中找到的字符串初始化的设置 bean。

  • 可能吗 ?
  • 你知道怎么做吗?
  • 知道我们在基础 Tomcat 上运行,使用 CDI 会更容易吗?(如果我需要将 tomcat 与 CDI 一起使用,我已经找到了这个)
4

1 回答 1

0

您能否尝试将所有与球衣 CDI 相关的 jar 添加到您的应用程序中?

于 2018-05-29T06:24:32.583 回答