Mongodb 在SpringMVC中为mongoTemplate实例获取空指针异常

Mongodb 在SpringMVC中为mongoTemplate实例获取空指针异常,mongodb,spring-mvc,Mongodb,Spring Mvc,我正在使用Spring和MongoDB创建一个简单的登录功能。在我的spring-context.xml中,我定义了mongo的配置,但当我在控制器中使用它时,它显示空指针异常 Spring-context.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/20

我正在使用Spring和MongoDB创建一个简单的登录功能。在我的spring-context.xml中,我定义了mongo的配置,但当我在控制器中使用它时,它显示空指针异常

Spring-context.xml

<?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"
        xmlns:c="http://www.springframework.org/schema/c" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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
         http://www.springframework.org/schema/data/mongo
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util-3.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="Controller" />

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Forwards requests to the "/" resource to the "welcome" view -->
    <mvc:view-controller path="/" view-name="welcome"/>

    <!-- Configures Handler Interceptors -->    
    <mvc:interceptors>
        <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptors>
<mongo:mongo host="localhost" port="27017"> 
        <mongo:options
             connections-per-host="5"            
             connect-timeout="30000"
             max-wait-time="10000"           
             write-number="1"
             write-timeout="0"
             write-fsync="true"/>
    </mongo:mongo>

    <mongo:db-factory dbname="Test"
                      mongo-ref="mongo"
                     /> 

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
       <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
     </bean>     
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Saves a locale change using a cookie -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>

有人能告诉我此错误的根本原因吗?

看起来您错过了控制器中的
@Autowired
注释

尝试:

或直接自动关联字段:

@Controller
public class LoginController {

    @Autowired
    private MongoTemplate mongoTemplate;
@Controller
public class LoginController {

    private MongoTemplate mongoTemplate;

    @Autowired
    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }
@Controller
public class LoginController {

    @Autowired
    private MongoTemplate mongoTemplate;