I have a Restful design for my Maven project, a domain layer with a user class, a data layer with the services with CRUD functionality and a web layer with the controller classes. But when I run the project I get the errors below:
javax.servlet.ServletException: Servlet.init() for servlet appServlet threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
........
java.lang.Thread.run(Thread.java:680)
root cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested
exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: ........service.UserService org.
bk.samples.mvc.UserController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [se.
datalayer.guards.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency
annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) ..........
root cause
org.springframework.beans.factory.BeanCreationException: Could not autowire field: .........service.UserService
org.bk.samples.mvc.UserController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type
[se.datalayer.guards.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency
annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} ..........
root cause
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [.........service.UserService] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) .......
Then I removed the @Autowired annotation but got the error below:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.NullPointerException
org.bk.samples.mvc.UserController.allUsers(UserController.java:32)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)..........
My userController class is:
@Controller
@RequestMapping(value="/")
public class UserController
{
@Autowired
UserService userService;
@RequestMapping(value="/users", method=RequestMethod.GET)
public ModelAndView allUsers()
{
ModelAndView mvd= new ModelAndView();
Collection<User> allUsers= new ArrayList<User>();
allUsers= userService.findAllUsers();
System.out.println(allUsers);
mvd.addObject("allUsers", allUsers);
return mvd;
}
}
I don’t know what caused the problem and I would appreciate any help.
It has nothing to do with Maven. You have problems with your Spring configuration. It’s telling you that you have no Spring bean of type UserService defined, so it can’t create your controller bean, since it depends on having a UserService (as expressed through the @Autowired annotation). You might want to have a look at an overview of the Spring container and/or the Spring MVC framework.