Question

I'm trying to make a MongoOperations Bean, but can't get it to work. I have several classes that will connect to a database, and I don't want to add the following code to every connection to the database.

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig2.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

The above code works..

I'm making a REST web service, and I have several classes that each need to write to a MongoDb.Collection.

I have set up a Bean configuration file:

@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();
    }

And then in the in mongodboperations class I try to Autowire it like this:

@Autowire MongoOperations mongoOperations2;

mongoOperations2.findAll(MongoLicense.class);

My error is that it cant create the bean. Can anyone see what I'm doing wrong?

Below is stack trace:

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)
Was it helpful?

Solution

in spring application context

<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>

and then in your classes

@Autowired
MongoTemplate mt;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top