如题,小弟今天遇到了一个AutowiringRequestProcessor不能自动完成注入service实例的问题,得到的service是空的,不管使用byName和byType都不行。如果使用ApplicationContext ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getSession()
.getServletContext());
pUserExtService=(IPUserExtService)ctx.getBean("pUserExtService");获得service,以下程序能正常运行。现帖出源码,希望各位能帮忙解决,谢谢!
web.xml
<web-app xmlns="<a href=" http:="" java.sun.com="" xml="" ns="" j2ee"="">http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
.............
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/*Context*.xml</param-value>
</context-param>
..............
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
.........................
</web-app>
RegeditAction.java
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
RegeditForm regeditForm = (RegeditForm) form;// TODO Auto-generated method stub
return null;
}
/**
* Ajax方法,用于验证用名户是否已被注册
* @param name
* @param request
* @return
*/
public boolean validateUser(String name,HttpServletRequest request)
{
// ApplicationContext ctx = WebApplicationContextUtils
// .getRequiredWebApplicationContext(request.getSession()
// .getServletContext());
// pUserExtService=(IPUserExtService)ctx.getBean("pUserExtService");
DetachedCriteria dc=DetachedCriteria.forClass(PUserExt.class);
dc.add(Restrictions.eq("account", name));
List list=this.getPUserExtService().findByDetachedCriteria(dc);
if(list!=null && list.size()>0)
{
return true;
}
return false;
}
BaseAction.java
public class BaseAction extends Action {
private static final Log log = LogFactory.getLog(BaseAction.class);
protected IPUserExtService pUserExtService;
protected ITblNewsService tblNewsService;
public IPUserExtService getPUserExtService() {
return pUserExtService;
}
public ITblNewsService getTblNewsService() {
return tblNewsService;
}
public void setTblNewsService(ITblNewsService tblNewsService) {
this.tblNewsService = tblNewsService;
}
public void setIPUserExtService(IPUserExtService pUserExtService) {
this.pUserExtService = pUserExtService;
}
...............
}
struts-config.xml
http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.poweroa.Fusion.struts.form.LoginForm">
<form-bean name="regeditForm" type="com.poweroa.Fusion.struts.form.RegeditForm">
</form-beans>
<global-exceptions>
<global-forwards>
<action-mappings>
........
</action-mappings>
<controller processorclass="org.springframework.web.struts.AutowiringRequestProcessor">
<message-resources parameter="com.poweroa.Fusion.struts.ApplicationResources">
</struts-config>
serviceContext.xml
<beans xmlns="<a href=" http:="" www.springframework.org="" schema="" beans"="">http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
..........
<bean id="pUserExtService" parent="baseTransactionProxy">
<property name="target">
<bean class="com.poweroa.Fusion.service.application.PUserExtService" init-method="init" autowire="byName">
</property>
</bean>
..............
</beans>