Extjs+structs2+spring+ibatis js调Action的方法内使用了spring注入的对象null的解决方案
2012-09-28 11:05
357 查看
背景:最近需要急速开发一个web聊天室,但是总出现spring注入对象空指针的情况,描述如下:
dwr.xml
structs.xml中
spring配置
UserAction中
MessageAction中
启动服务器,显示:
-----UserDAO inserted ok------
----UserService insertted---
1.问题:User和Message同样的配置,但是只有User被注入进来了。
刚开始以为是配置的代码里面有什么小的出入,仔细比对后并没有发现什么异常。后来发现之所以在启动服务器的时候UserAction会被注入,是因为在structs.xml中使用到了UserAction。所以在启动时就注入了UserService。
尝试:在structs.xml中加入类似的代码,也引入MessageAction,一边其注入MessageService.
重启服务器,显示:
信息: 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修改如下:
再次重启服务器,运行,一切OK!
提示:(之所以在UserAction中 设置static 是因为程序是通过structs调用的UserAction中的对象,而在服务器启动时候已经注入了userService,所以可以用;然而,使用MessageService的addPublicService方法时,是使用extjs内部ajax机制,使用的messageService和Structs注入的不是同一个对象,所以会报空指针,设置成为静态后,就行了)
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注入的不是同一个对象,所以会报空指针,设置成为静态后,就行了)
相关文章推荐
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- Spring 使用注解注入,调用类实例出现 java.lang.NullPointException 问题的解决方法
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- Spring: 读取 .properties 文件地址,json转java对象,el使用java类方法相关 (十三)
- spring入门(6)---set方法注入依赖之null的注入
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- spring中使用@RequestBody,对应的bean中属性名大写注入为空的解决方法
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 使用AJAX调用Spring controller 并返回map对象,@ResponseBody 只能返回string的解决方案:
- Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
- 使用Spring(八) 延迟初始化bean、Lookup方法注入
- spring学习(官网)——使用工厂方法实例化对象
- java.lang.IllegalAccessException Action中的方法调用了,却不出现结果 spring注入值为null
- spring使用体会——sesionFactory注入的空指针异常(nullpointException)
- spring.net的简单使用(四)对象属性注入
- 关于怎么解决从ajax传入的json参数注入到Controller的接收对象 以及如何在Spring Boot项目使用参数校验
- 控制层注入对象 已经注入进去了 在某个方法调用发现为null