我很难把一个javax.servlet.ServletConfig放到一个带有org.springframework.context.annotation.Configuration注释的类中。
我的团队决定我们应该使用spring进行依赖注入,我正在尝试使用它迁移我们的一个简单的Rest服务。
我的限制是:
org.springframework:spring-context。web.xml中定义了一个字符串参数。我需要得到它,用它实例化一个Bean,并在代码中的几个位置注入结果bean。我已经拥有的是:
代码使用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;
}
}我需要CContextBeanProvider中的servlet配置,如下所示:
@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。
发布于 2018-05-29 06:24:32
你能试着把所有的球衣CDI相关的罐子添加到你的应用程序中吗?
https://stackoverflow.com/questions/50560948
复制相似问题