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;
}
}
何が問題の原因なのかわからないので、助けていただければ幸いです。