Extjs+structs2+spring+ibatis js调Action的方法内使用了spring注入的对象null的解决方案

背景:最近需要急速开发一个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 start

2.问题:现在两个Service是被注入了,但是登录进入聊天室以后,发现在MessageService对象还是空指针。

尝试:既然在程序启动的时候已经注入了,现在却又变成null,说明这两次用的不是同一个UserService对象,何不将其设置为static呢,这样在启动服务器的时候就行,以后就再也不用担心了,MessageAction修改如下:

private static MessageService messageService;

再次重启服务器,运行,一切OK!

提示:(之所以在UserAction中 设置static 是因为程序是通过structs调用的UserAction中的对象,而在服务器启动时候已经注入了userService,所以可以用;然而,使用MessageService的addPublicService方法时,是使用extjs内部ajax机制,使用的messageService和Structs注入的不是同一个对象,所以会报空指针,设置成为静态后,就行了)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值