【发布时间】:2016-05-13 23:39:51
【问题描述】:
我使用 Apache CXF 3 和 Spring 3 开发了基于 SOAP 的 Web 服务,并部署在 Tomee 上。我有 2 个 xml ( 1. beans.xml(cxf service) 2. spring-servlt.xml)。我在我的 cxf 服务 xml 中引用了一些 DAO 层 bean。它没有被注入。当我对 CXFServlet 类进行分析时。 loadBus 方法从 WebApplicationContextUtils 类获取应用程序上下文。
它在 Jetty 中运行良好。但它在 Tomeeif 中不起作用。如果我使用 ContextLoaderListener 加载 cxf bean,则不会公开 Web 服务。之后我使用了配置位置。对于码头,我没有为 cxf 使用单独的 xml。我在我的应用程序 bean 中合并了 cxf bean,它由调度程序 servlet 加载并且运行良好
CXFServlet.java
protected void loadBus(ServletConfig sc) {
ApplicationContext wac = WebApplicationContextUtils.
getWebApplicationContext(sc.getServletContext());
private ApplicationContext createSpringContext(ApplicationContext ctx,
ServletConfig servletConfig,
String location) {
XmlWebApplicationContext ctx2 = new XmlWebApplicationContext();
createdContext = ctx2;
ctx2.setServletConfig(servletConfig);
Resource r = ctx2.getResource(location);
try {
InputStream in = r.getInputStream();
in.close();
} catch (IOException e) {
//ignore
r = ctx2.getResource("classpath:" + location);
try {
r.getInputStream().close();
} catch (IOException e2) {
//ignore
r = null;
}
}
try {
if (r != null) {
location = r.getURL().toExternalForm();
}
} catch (IOException e) {
//ignore
}
if (ctx != null) {
ctx2.setParent(ctx);
String names[] = ctx.getBeanNamesForType(Bus.class);
if (names == null || names.length == 0) {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
} else {
ctx2.setConfigLocations(new String[] {location});
}
} else {
ctx2.setConfigLocations(new String[] {"classpath:/META-INF/cxf/cxf.xml",
location});
createdContext = ctx2;
}
ctx2.refresh();
return ctx2;
}
如果 (ctx != null) { ctx2.setParent(ctx);
由于 ctx (wac) 为 null,cxf 无法设置父上下文。 bean 注入不起作用。请指导我为什么这个 wac 为空。
Web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/connector/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
我尝试了所有选项,大多数选项都已以形式提供。 我是否需要在 Tomee 或我的代码中添加任何参数。
【问题讨论】:
-
有没有在web.xml中添加ContextLoaderListener
-
如果我使用 ContextLoaderListener 加载 cxf bean,Web 服务不会暴露。之后我使用了配置位置。它在码头工作得很好。但它在 Tomee 中不起作用。
标签: java spring cxf apache-tomee