问题描述:
在做WebService的时候,用CFX技术和Spring进行整合,然后通过WebService的方法调用业务逻辑代码,发现用Spring的依赖注入没有成功(失效)。去调web.xml中有关WebService代码,程序运行成功。
web.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beanRefServer.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--
默认的spring配置文件是在WEB-INF下的applicationContext.xml
-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value><!-- 强制进行转码 -->
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 默认所对应的配置文件是WEB-INF下的{servlet-name}-servlet.xml,这里便是:spring3-servlet.xml -->
<servlet>
<servlet-name>spring3</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring3</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Controller代码
@Controller
@RequestMapping("/user")
public class UserController {
private IUserService userServer;
private IDeptService deptServer;
public IDeptService getDeptService() {
return deptServer;
}
public void setDeptService(IDeptService deptServer) {
this.deptServer = deptServer;
}
public IUserService getUserService() {
return userServer;
}
public void setUserService(IUserService userServer) {
this.userServer = userServer;
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(@RequestParam String name,@RequestParam String password,Model model,HttpServletRequest request) throws Exception{