【发布时间】:2015-04-10 20:45:05
【问题描述】:
我想将 Jersey 与 Spring 一起使用。我正在开发独立应用程序(未部署到某些网络服务器,而是作为控制台应用程序运行)。所以我做了这样的事情:
public static void main(String[] args) throws IOException {
ResourceConfig rc = new PackagesResourceConfig(MAIN_PACKEAGE);
HttpServerFactory.create(BASE_URI, rc).start();
}
效果很好,但接下来我尝试将 Jersey Resources 与 Spring 集成。正如here 所提到的,我应该将 jersey-spring3 添加到类路径中,因此我将依赖项添加到 pom.xml:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.17</version>
</dependency>
但它实际上并没有工作:它甚至没有初始化 Spring 上下文(参考日志)。我尝试手动初始化 SC:
new ClassPathXmlApplicationContext(PATH_TO_APP_CONTEXT);
这有效,但单独:上下文正在初始化,但依赖注入不起作用 - 我无法将某些 bean 自动装配到 Jersey 资源(也标记为 Spring 组件)。
更新
我发现 jersey-spring3 非常适合 Grizzly 服务器。所以工作代码示例是:
public static void main(String[] args) throws IOException {
ResourceConfig rc = new ResourceConfig().packages(MAIN_PACKAGE);
GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
但我仍然想知道如果我使用 HttpServerFactory.create(BASE_URI, rc) 创建服务器,为什么与 Spring 的集成不起作用...
【问题讨论】:
-
如果您能提供更多有关问题的背景信息?
-
在您的 applicationContext.xml 中是否启用了组件扫描?
-
当然可以。 Spring 完美初始化,但它的上下文没有绑定到 Jersey 上下文。