背景:最近需要急速开发一个web聊天室,但是总出现spring注入对象空指针的情况,描述如下:
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="MessageActionJS">
<param name="class" value="com.downjoy.app.chatsystem.common.action.MessageAction" />
</create>
<create creator="new" javascript="UserActionJS">
<param name="class" value="com.downjoy.app.chatsystem.common.action.UserAction" />
</create>
<!-- when an object(bean) was used as a parameter, it should be coverted like this -->
<convert converter="bean" match="com.downjoy.app.chatsystem.common.to.SessionMessageTO" />
<convert converter="bean" match="com.downjoy.app.chatsystem.common.to.OnlineUserTO" />
</allow>
</dwr>
structs.xml中
<!-- 用户管理 -->
<action name="login" class="userAction" method="login">
<result name="success" type="redirect">jsp/chat.jsp</result>
<result name="input">index.jsp</result>
</action>
<action name="logout" class="userAction" method="logout">
<!-- <result type="redirect">index.jsp</result> -->
</action>
spring配置
<bean name="userAction" class="com.downjoy.app.chatsystem.common.action.UserAction" scope="prototype"> <property name="userService" ref="userService" /> </bean> <bean name="treeProAction" class="com.downjoy.app.chatsystem.common.action.TreeProAction" scope="prototype"/> <bean name="messageAction" class="com.downjoy.app.chatsystem.common.action.MessageAction" scope="prototype"> <property name="messageService" ref="messageService" /> </bean>
<bean id="messageDAO" class="com.downjoy.app.chatsystem.common.dao.ibatis.MessageDAOImpl"/> <bean id="userDAO" class="com.downjoy.app.chatsystem.common.dao.ibatis.UserDAOImpl"/> <bean id="messageService" class="com.downjoy.app.chatsystem.common.service.MessageServiceImpl"> <property name="messageDAO" ref="messageDAO"/> </bean> <bean id="userService" class="com.downjoy.app.chatsystem.common.service.UserServiceImpl"> <property name="userDAO" ref="userDAO"/> </bean>
UserAction中
private UserService userService;
public void setUserService(UserService userService) { this.userService = userService; System.out.println("----UserService insertted---"); }
MessageAction中private MessageService messageService;
public void setMessageService(MessageService messageService) { this.messageService = messageService; System.out.println("insert MessageService ok++++++++++++++"); }
启动服务器,显示:-----UserDAO inserted ok------
----UserService insertted---1.问题:User和Message同样的配置,但是只有User被注入进来了。
刚开始以为是配置的代码里面有什么小的出入,仔细比对后并没有发现什么异常。后来发现之所以在启动服务器的时候UserAction会被注入,是因为在structs.xml中使用到了UserAction。所以在启动时就注入了UserService。
尝试:在structs.xml中加入类似的代码,也引入MessageAction,一边其注入MessageService.
<action name="addPublicMessage" class="messageAction" method="addPublicMessage"/>
重启服务器,显示:信息: Initializing Spring root WebApplicationContext
---MessageDAO insertted ok----
-----UserDAO inserted ok------
----UserService insertted---
insert MessageService ok++++++++++++++
2012-9-28 10:26:01 org.apache.coyote.http11.Http11Protocol start2.问题:现在两个Service是被注入了,但是登录进入聊天室以后,发现在MessageService对象还是空指针。
尝试:既然在程序启动的时候已经注入了,现在却又变成null,说明这两次用的不是同一个UserService对象,何不将其设置为static呢,这样在启动服务器的时候就行,以后就再也不用担心了,MessageAction修改如下:
private static MessageService messageService;
再次重启服务器,运行,一切OK!提示:(之所以在UserAction中 设置static 是因为程序是通过structs调用的UserAction中的对象,而在服务器启动时候已经注入了userService,所以可以用;然而,使用MessageService的addPublicService方法时,是使用extjs内部ajax机制,使用的messageService和Structs注入的不是同一个对象,所以会报空指针,设置成为静态后,就行了)