我使用Apache CXF 3和Spring 3開發了基於SOAP的Web服務,並部署在Tomee上。我有2個XML(1. beans.xml(cxf服務)2. spring-servlt.xml)。我在我的cxf服務xml中引用了一些DAO層bean。它沒有被注射。當我分析了CXFServlet類時。 loadBus方法從WebApplicationContextUtils類獲取應用程序上下文。Apache CXF spring bean未注入
它在Jetty中正常工作。但它不在Tomeeif工作。如果我使用ContextLoaderListener加載cxf bean,則Web服務不會公開。之後我使用了config-location。對於碼頭,我沒有使用單獨的xml作爲cxf。我在我的應用豆合併CXF豆,它是由調度員的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)爲空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或我的代碼中添加任何參數。
如果我使用ContextLoaderListener加載cxf bean,您是否已經將ContextLoaderListener添加到web.xml – andy
,Web服務未公開。之後我使用了config-location。它在碼頭工作正常。但它不在Tomee工作。 – Gnana