我的Maven项目有Restful设计,带有用户类的域层,带有CRUD功能的服务的数据层和带有控制器类的Web层。但是当我运行项目时,我得到以下错误:
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) .......
然后我删除了@Autowired
注释,但得到了以下错误:
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)..........
我的userController
课程是:
@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;
}
}
我不知道是什么导致了这个问题,我将不胜感激。
答案 0 :(得分:0)
它与Maven无关。您的Spring配置有问题。它告诉你没有定义UserService类型的Spring bean,所以它不能创建你的控制器bean,因为它依赖于具有UserService(通过@Autowired注释表示)。您可能需要查看overview of the Spring container和/或Spring MVC framework。
答案 1 :(得分:0)
您的网络应用中可能存在弹簧配置错误,与maven无关。无论你在哪里引导spring,你都可能包括你的webapp配置,但不包括你的数据层spring bean。