Spring mvc mongodb 注入dao层 报空指针

J2EE 码拜 10年前 (2015-04-10) 2615次浏览 0个评论

service层文件:
public class FnMongoEntityLogicImpl implements FnEntityLogic {

  private BaseMongoDao       dao;

public BaseMongoDao getDao() {

        return dao;
    }

    public void setDao(BaseMongoDao dao) {

        this.dao = dao;
    }

@Override
    public void addEntityList(List<FnEntity> entityList) {
        dao.addEntityList(entityList);
    }

}

dao层文件:
public class BaseMongoDaoImpl  implements BaseMongoDao {

private static final Log   logger           = LogFactory.getLog(BaseMongoDaoImpl.class);

protected MongoTemplate mongoTemplate;

//get、set方法省略了。。。。
   @Override
public String addEntity(FnEntity entity) {
entity.initAddInfo();
//Write operations that use this write concern will wait for acknowledgement from the primary server before returning.
mongoTemplate.setWriteConcern(WriteConcern.FSYNC_SAFE);
mongoTemplate.insert(entity);
// mongoTemplate.save(entity, collectionName);
return entity.getId();
}
}

配置文件:
 <bean id=”baseMongoDao” class=”com.moxi.forum.framework.mongo.dao.BaseMongoDaoImpl”>
        <!–<constructor-arg name=”mongoDbFactory” ref=”mongoDbFactory” />–>
         <property name=”mongoTemplate” ref=”mongoTemplate”></property>
    </bean> 
    <bean id=”mongoLogic” class=”com.moxi.forum.framework.logic.FnMongoEntityLogicImpl”>             
         <property name=”dao” ref=”baseMongoDao”></property>
         <property name=”pageDao” ref=”fnPageBaseMongoDao”></property> 
           <property name=”fileDao” ref=”fileMongoDao”></property>
    </bean>

2014-09-28 16:33:18,751 DEBUG [com.moxi.forum.framework.interceptor.servlet.FnDispatcherServlet] – Handler execution resulted in exception – forwarding to resolved error view: ModelAndView: reference to view with name “”/error””; model is {exception=java.lang.NullPointerException}
java.lang.NullPointerException
at com.moxi.forum.framework.logic.FnMongoEntityLogicImpl.addEntity(FnMongoEntityLogicImpl.java:64)
at com.moxi.forum.post.logic.PostingLogicImpl.addPost(PostingLogicImpl.java:29)
at com.moxi.forum.post.controller.PostController.addPost(PostController.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at com.moxi.forum.framework.interceptor.servlet.FnDispatcherServlet.doService(FnDispatcherServlet.java:28)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
2014-09-28 16:33:18,761 DEBUG [com.moxi.forum.framework.interceptor.servlet.FnDispatcherServlet] – Rendering view [org.springframework.web.servlet.view.JstlView: name “”/error””; URL [/page//error.jsp]] in DispatcherServlet with name “”spring””
2014-09-28 16:33:18,818 DEBUG [com.moxi.forum.framework.interceptor.servlet.FnDispatcherServlet] – Successfully completed request

Spring mvc mongodb 注入dao层 报空指针
baseMongoDao 这个bean id 定义了吗?
Spring mvc mongodb 注入dao层 报空指针
bean id 是什么东东?,能详细点吗?
Spring mvc mongodb 注入dao层 报空指针
54分
<bean id=”mongoLogic” class=”com.moxi.forum.framework.logic.FnMongoEntityLogicImpl”>             
         <property name=”dao” ref=”baseMongoDao”></property>
         <property name=”pageDao” ref=”fnPageBaseMongoDao”></property> 
           <property name=”fileDao” ref=”fileMongoDao”></property>
</bean>

mongoLogic 引用  <property name=”dao” ref=”baseMongoDao”></property> 那baseMongoDao 必须先定义了,然后先初始化的
spring bean的初始化顺序,有依赖的,依赖的bean先初始化,然后再引用进来

Spring mvc mongodb 注入dao层 报空指针
对的,我是先注入的,我拷贝代码的时候先放mongoLogic了,sorry,应该不是这个问题。。。。
Spring mvc mongodb 注入dao层 报空指针
引用 4 楼 zt3124 的回复:

对的,我是先注入的,我拷贝代码的时候先放mongoLogic了,sorry,应该不是这个问题。。。。

拷贝代码,先放谁后放谁没有关系
只要配置文件和代码都在,spring会去找的

Spring mvc mongodb 注入dao层 报空指针
6分
FnMongoEntityLogicImpl.java:64
这一行代码是什么?

另外启动的时候没有报错是吧?

Spring mvc mongodb 注入dao层 报空指针
你的配置文件里“mongoTemplate”配置在哪儿叫?
Spring mvc mongodb 注入dao层 报空指针
3个bean没看到啊
fileMongoDao
fnPageBaseMongoDao
mongoTemplate

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Spring mvc mongodb 注入dao层 报空指针
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!