我正在使用Spring和MongoDB创建一个简单的登录功能。在我的spring-context.xml中,我定义了mongo的配置,但是当我在我的控制器中使用它时,它显示空指针Exception。
弹簧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>
控制器:
@Controller
public class LoginController {
private MongoTemplate mongoTemplate;
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
private DBCollection getCollection(String collectionName)
{
System.out.println("MongoTemplate"+mongoTemplate);
return mongoTemplate.getCollection(collectionName);
}
@RequestMapping(value="/addU", method=RequestMethod.POST)
public @ResponseBody String add(@ModelAttribute("AddUser")User user, BindingResult result, ModelMap model)
{
String returnText="";
System.out.println("Inside response");
DBCollection table=getCollection("User");
BasicDBObject document = new BasicDBObject();
document.put("name", user.getName());
document.put("password", user.getPassword());
DBCursor cur = table.find(document);
if(cur.hasNext())
{
returnText="User Found Successfully";
}
else
{
returnText="User not Found";
}
return returnText;
}
}
有人能告诉我这个错误的根本原因吗?
答案 0 :(得分:2)
您好像错过了控制器中的@Autowired
注释。
尝试:
@Controller
public class LoginController {
private MongoTemplate mongoTemplate;
@Autowired
public void setMongoTemplate(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
直接自动装配该字段:
@Controller
public class LoginController {
@Autowired
private MongoTemplate mongoTemplate;