我正在尝试制作一个MongoOperations Bean,但无法让它工作。我有几个将连接到数据库的类,我不想将以下代码添加到数据库的每个连接。
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig2.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
以上代码有效..
我正在制作一个REST Web服务,我有几个类,每个类都需要写入MongoDb.Collection。
我已经设置了一个Bean配置文件:
@Configuration
public class SpringMongoConfig2 {
@Bean
public MongoDbFactory mongoDbFactory2() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(), "myDatabase");
}
@Bean
public MongoTemplate mongoTemplate2() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory2());
return mongoTemplate;
}
@Bean
public MongoOperations mongoOperations2() throws Exception {
return mongoTemplate2();
}
然后在mongodboperations类中,我试着像这样自动装配它:
@Autowire MongoOperations mongoOperations2;
mongoOperations2.findAll(MongoLicense.class);
我的错误是它无法创建bean。谁能看到我做错了什么?
下面是堆栈跟踪:
type Exception report
message Request processing failed; nested exception is java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:943)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
no.nlf.rest.CorsFilter.doFilter(CorsFilter.java:27)
root cause
java.lang.NullPointerException
no.nlf.dal.LicenseController.getAllLicenses(LicenseController.java:30)
no.nlf.logic.LicenseLogic.getAllLicenses(LicenseLogic.java:18)
no.nlf.rest.RestLicense.licenseList(RestLicense.java:26)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
no.nlf.rest.CorsFilter.doFilter(CorsFilter.java:27)
答案 0 :(得分:1)
在spring应用程序上下文中
<mongo:db-factory id="mongoDbFactory" host="db" port="27017" dbname="dbname" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
然后在你的课程中
@Autowired
MongoTemplate mt;