I'm relatively new to Java and coming from a web background I found myself trying to implement a web application in Spring.
The main problem is I have the following folder structure:
And I am trying to inject Spring context into any class found in the Controllers folder, which basically, as far as I understood means that my classes are not Spring-dependant.
The application uses MongoDB as a backend, and I am trying to instantinate the context like this:
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
IODataInterface mongo = context.getBean(BookModel.class);
I'm getting this exception on the context.getBean(...) line: com.web.spring.Controllers.DemoController.DemoController(DemoController.java:19)
Any help is appreciated and I'd be really glad if someone could properly explain me the actual terminology as, you might have guessed already, I have jumped into some pretty advanced stuff for someone with 0 Java work experience.
Full exception stack:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
And as far as I see, the context seems to get created, but the getBean() doesn't work. I'm really confused now.
You seem to be trying to get an instance of a model class (annotated by @Document
I imagine, since you're using MongoDb). @Document
and JPA @Entity
annotated classes are actually not Spring Beans.
So to instantiate a new one you can just use new BookModel()
.
PS. as an aside, please adopt a low case package/folder naming convention. Only class names should start with a capital letter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.