【问题标题】:Initialize Spring beans when running Jersey test运行 Jersey 测试时初始化 Spring bean
【发布时间】:2016-08-08 07:00:24
【问题描述】:
我正在使用最新的 Spring 和 Jersey 测试框架。
我所有的资源都使用泽西岛。
我有由 @Service 注释初始化的 spring bean。
每个服务都将自己注册到一个客户 ServiceContainer,它是一个 bean 单例。
在我的资源中,我从容器中获取服务并使用注释。
我正在使用 Jersey 框架运行单元测试。如何使用 @Service 的注释初始化这些服务。我没有在我的资源中使用 @Autowire / @Inject 注释。
我看了几个类似的案例,但没有任何帮助,有什么想法吗?
【问题讨论】:
标签:
java
spring
spring-boot
jersey-2.0
spring-annotations
【解决方案1】:
这对我有用:
@Override
protected DeploymentContext configureDeployment() {
ResourceConfig config = new ResourceConfig();
config.packages("com.my.application");
return ServletDeploymentContext
.forServlet(new ServletContainer(config))
.addListener(ContextLoaderListener.class)
.contextParam("contextConfigLocation", "classpath:applicationContext.xml")
.build();
}