我正在尝试使用 Spring 应用程序上下文配置 Jersey (2.4) 测试,使用 Spring 配置文件。
我的测试课是这样的:
public class OrderControllerTest extends JerseyTest {
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
ApplicationResourceConfig config = new ApplicationResourceConfig();
config.property("contextConfigLocation", "classpath:spring-context.xml");
config.property("spring.profiles.active", "development");
return config;
}
...
这是哪里ApplicationResourceConfig
的课:
public class ApplicationResourceConfig extends ResourceConfig {
public ApplicationResourceConfig() {
register(RequestContextFilter.class)
.register(OrderController.class);
}
...
和OrderController
类是:
@Component
@Path("/order")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class OrderController {
@Autowired
private OrderService orderService;
@PUT
public Response createOrder(String order) {
return Response.ok().build();
}
...
问题是 DI 不工作(orderService
是null
)。DI 在其他测试中运行良好(不是基于 Jersey 的)。我想原因可能是没有正确加载 Spring 配置文件,因为 Spring 日志说:
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemProperties]
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [systemEnvironment]
426 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.profiles.active' in any property source. Returning [null]
请问有没有人可以就这个问题提供建议?