Spring Autowiring Service在我的Controller中不起作用

我有问题,以便在我的控制器中自动assembly服务。 我有这个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private es.unican.meteo.service.UserService es.unican.meteo.controller.MyController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [es.unican.meteo.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)} 

似乎userService没有注册,因此,控制器无法获取bean。 我认为我的配置没问题,因为它适用于测试。 在测试中我有这个:

 ClassPathXmlApplicationContext("/WEB-INF/app-config.xml"); 

我可以从ApplicationContext.xml中获取bean

我的包结构如下:

es.unican.meteo.controller

| —- MyController.java

es.unican.meteo.service

| —- UserService.java

es.unican.meteo.service.impl

| —- UserServiceImpl.java

…..

的WebContent / WEB-INF

| —- MyDispatcherServlet-servlet.xml

| —- app-config.xml

| —- web.xml

…..

条款:

== UserServiceImpl.java ==

 @Service public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } 

== MyController.java ==

 @Controller public class MyController { @Autowired private UserService userService; @RequestMapping(method=RequestMethod.GET, value="/home") public String handleRequest(){ return "welcome"; } @RequestMapping(method=RequestMethod.GET, value="/getUsers") public @ResponseBody List getUsersInJSON(){ return userService.getUsers(); } } 

== web.xml ==

 Spring MVC  MyDispatcherServlet org.springframework.web.servlet.DispatcherServlet   MyDispatcherServlet *.go   

== app-config.xml ===

                        

== MyDispatcherServlet.xml ==

            

Spring mvc logger trace:

 19:38:54,119 DEBUG http-8080-1 support.DefaultListableBeanFactory:430 - Creating instance of bean 'myController' 19:38:54,170 DEBUG http-8080-1 annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.controller.MyController]: AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.controller.MyController.userService 19:38:54,174 DEBUG http-8080-1 support.DefaultListableBeanFactory:504 - Eagerly caching bean 'myController' to allow for resolving potential circular references 19:38:54,206 DEBUG http-8080-1 annotation.InjectionMetadata:85 - Processing injected method of bean 'myController': AutowiredFieldElement for private es.unican.meteo.service.UserService es.unican.meteo.controller.MyController.userService 19:38:54,224 DEBUG http-8080-1 support.DefaultListableBeanFactory:217 - Creating shared instance of singleton bean 'userServiceImpl' 19:38:54,226 DEBUG http-8080-1 support.DefaultListableBeanFactory:430 - Creating instance of bean 'userServiceImpl' 19:38:54,234 DEBUG http-8080-1 annotation.InjectionMetadata:60 - Found injected element on class [es.unican.meteo.service.impl.UserServiceImpl]: AutowiredFieldElement for private es.unican.meteo.dao.UserMapper es.unican.meteo.service.impl.UserServiceImpl.userMapper 19:38:54,237 DEBUG http-8080-1 support.DefaultListableBeanFactory:504 - Eagerly caching bean 'userServiceImpl' to allow for resolving potential circular references 19:38:54,256 DEBUG http-8080-1 annotation.InjectionMetadata:85 - Processing injected method of bean 'userServiceImpl': AutowiredFieldElement for private es.unican.meteo.dao.UserMapper es.unican.meteo.service.impl.UserServiceImpl.userMapper 19:38:54,268 INFO http-8080-1 support.DefaultListableBeanFactory:433 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@56088b29: defining beans [myController,roleService,userServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy 19:38:54,279 ERROR http-8080-1 servlet.DispatcherServlet:457 - Context initialization failed 

我已经回顾了一些关于这个主题的问题,但我没有找到解决问题的方法。 也许我正在跳过一些东西,但我当然不知道。 我试图改变组件扫描没有结果。

当我尝试访问/SPRING-MVC/getUsers.go时出现那些错误。

我不知道bean是否必须放在app-config(applicationContext)或servlet.xml中,因为它有点令人困惑……

谢谢

你的配置很奇怪……

首先排除显而易见的问题

我没有在您的web.xml看到根Web应用程序上下文配置。 可能是你忘了添加这段代码?

  contextConfigLocation  WEB-INF/app-config.xml    org.springframework.web.context.ContextLoaderListener  

现在有点理论

Spring的理论 – Spring使用Web应用程序的应用程序上下文层次结构:

  • 顶级Web应用程序上下文由ContextLoaderListener加载
  • 然后每个DispatcherServlet实例都有单独的上下文

在实例化新bean时,它可以从定义它的上下文或从父上下文获取依赖项。 这使得在根上下文(服务,DAO,…)中定义公共bean成为可能,并且在servlet应用程序上下文中具有请求处理bean,因为每个servlet可以拥有自己的一组控制器,查看操作符,…

最后,但并非最不重要 – 你的错误

您正在根上下文中配置MVC。 那是错的。 从那里删除 context。

您还通过基础包上的在根上下文中注册控制器。 使组件仅在services包上扫描,或者将类分成两个顶级包core (对于根bean)和servlet (对于servlet bean)。

确保您的UserServiceImplcontext:component-scan定义的包相同context:component-scan 。 如果不是,spring将无法检测到它。 另外,尝试从UserServiceImpl定义中删除value属性,因为该类型只有1个bean。 Spring将能够按类型自动assembly它。

您需要更改在控制器中自动assembly服务的方式。

更改以下代码

 @Autowired private UserService userService; 

以下

 @Resource(name="userService") private UserService userService; 

因为在UserServiceImpl中,您已使用别名“userService”定义了@Service批注。

我希望这能解决你的问题。 🙂

当你面对这样的问题时,请检查一下上下文的路径:组件扫描basepackage

它应该是root name就像我将com.mike作为包名并且在其结构中包含bean,controller,dao,service文件夹一样,在这种情况下你必须遵循Like —- context:component-scan basepackaage = “com.mike。*”

其中*表示将扫描所有文件夹(bean,service,dao,controller和theie对应的类)。

您可以使用@Qualifier注释,如下所示:

 @Autowired @Qualifier("userService") private UserService userService; 

乍一看,配置似乎没问题,但可能会有一些较小的绊网可能不那么明显。

a)实现UserService接口,是否与控制器需要相同? 愚蠢的问题,我知道,但只是在安全的一面。

b)bean名称:尝试从@Service注释中删除值-value(ba-da-tush),无论如何都是超级的。 或者在@Qualifier的帮助下更具体。

c)包扫描:仔细检查您实施的服务是否真的在es.unican.meteo内。 有时是小事。