我正在尝试制作 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)