好久没用Spring了。注入不进去是怎样啊..求解。

yoyoyo0 2013-01-18 10:28:06
用的Mybatis+Spring
贴出我的applicationContext配置、



?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="autoPage" />

<bean id="commentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mimi.dao.CommentDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="commentService" class="com.mimi.service.impl.CommentServiceImpl">
<property name="commentDao" ref="commentDao"></property>
</bean>
<bean id="commentModule" class="com.mimi.module.CommentModule">
<property name="commentService" ref="commentService"></property>
</bean>




只贴出的了重点。


然后我在Action(commentModule)中是这么用的。

private CommentService commentService;

public CommentService getCommentService() {
return commentService;
}

public void setCommentService(CommentService commentService) {
this.commentService = commentService;
}




然后就NullPointerExeception 了。
指定是注入失败。

因为的在Class中手动getBean测试都成功了。。

求大神指点,我是哪里不对。还是思路有问题。谢谢
...全文
218 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoyoyo0 2013-01-18
  • 打赏
  • 举报
回复
引用 7 楼 tianlong0388 的回复:
引用 5 楼 suciver 的回复:你Action的主人在哪里,没看到你的spring里面有action的bean 你用的是注解吗?先看看action
我没用注解方式。。。是想自动注入的。。
yoyoyo0 2013-01-18
  • 打赏
  • 举报
回复
引用 9 楼 aiqinbird 的回复:
你spring文件在web.xml中加了没?
<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext.xml,classpath:applicationContext.xml,classpath*:applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
Xml是这样配置的。。。
gqchen1981 2013-01-18
  • 打赏
  • 举报
回复
你spring文件在web.xml中加了没?
yoyoyo0 2013-01-18
  • 打赏
  • 举报
回复
引用 5 楼 suciver 的回复:
你Action的主人在哪里,没看到你的spring里面有action的bean
我用的是Module 。基于Servlet的类。 那个CommentModule就类似于Action
a092cc 2013-01-18
  • 打赏
  • 举报
回复
注入的话要加@resource吧, 另外你这句是干什么的<context:component-scan base-package="autoPage" />。 你的base-package不是autoPage吧
无情天下 2013-01-18
  • 打赏
  • 举报
回复
引用 5 楼 suciver 的回复:
你Action的主人在哪里,没看到你的spring里面有action的bean
你用的是注解吗?先看看action
明天_过後 2013-01-18
  • 打赏
  • 举报
回复
suciver 2013-01-18
  • 打赏
  • 举报
回复
你Action的主人在哪里,没看到你的spring里面有action的bean
yoyoyo0 2013-01-18
  • 打赏
  • 举报
回复
引用 1 楼 wyatou 的回复:
你的struts.xml文件里面是怎么配置的?可以贴出来看看。。。。 有可能是你的struts.xml文件里面action配置写错了。。。还有就是你的commentService在action里面只需要有set方法就可以了,不需要get方法。。。 你贴出来的这些貌似没有错误。。。
我没用Struts啊。。。
  • 打赏
  • 举报
回复
看配置没什么问题,要不试试注解方式
BUG胡汉三 2013-01-18
  • 打赏
  • 举报
回复
这个配置还真没有问题~~~!!! 人品啊哥们、你换一换名称试试!!! commentService换成commentToService什么的、然后在试试~~~有时候我遇到这些莫名奇妙的问题、就是这样、换名称、尽量区别开来!
wyatou 2013-01-18
  • 打赏
  • 举报
回复
你的struts.xml文件里面是怎么配置的?可以贴出来看看。。。。 有可能是你的struts.xml文件里面action配置写错了。。。还有就是你的commentService在action里面只需要有set方法就可以了,不需要get方法。。。 你贴出来的这些貌似没有错误。。。

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧