用的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测试都成功了。。求大神指点,我是哪里不对。还是思路有问题。谢谢

解决方案 »

  1.   

    你的struts.xml文件里面是怎么配置的?可以贴出来看看。有可能是你的struts.xml文件里面action配置写错了还有就是你的commentService在action里面只需要有set方法就可以了,不需要get方法你贴出来的这些貌似没有错误
      

  2.   

    这个配置还真没有问题~~~!!!
        人品啊哥们、你换一换名称试试!!!
    commentService换成commentToService什么的、然后在试试~~~有时候我遇到这些莫名奇妙的问题、就是这样、换名称、尽量区别开来!
      

  3.   

    你Action的主人在哪里,没看到你的spring里面有action的bean
      

  4.   


    你用的是注解吗?先看看action
      

  5.   

    注入的话要加@resource吧,
    另外你这句是干什么的<context:component-scan base-package="autoPage" />。
    你的base-package不是autoPage吧
      

  6.   

    你spring文件在web.xml中加了没?
      

  7.   


    我用的是Module 。基于Servlet的类。 那个CommentModule就类似于Action
      

  8.   

    <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是这样配置的