org.springframework.beans.factory.BeanCreationException:无法自动装配字段:

时间:2016-11-26 05:32:15

标签: spring maven spring-mvc intellij-idea mybatis

我遇到问题:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hdx.ssm.service.ItemsService com.hdx.ssm.controller.ItemsController.itemsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hdx.ssm.service.ItemsService] 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)}

这是ItemsController:

@Controller
@RequestMapping("/items")
public class ItemsController {
    @Autowired
    private ItemsService itemsService;

和ItemsService:

  public interface ItemsService {

实施班:

public class ItemsServiceImpl implements ItemsService {
   @Autowired
   private ItemsMapperCustom itemsMapperCustom;
    @Autowired
    private ItemsMapper itemsMapper;

还有一些设置spring-mvc.xml:

  <context:component-scan base-package="com.hdx.ssm.controller"/>

    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>


    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/> 
            </list>
        </property>
    </bean>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您需要将@Service添加到ItemsServiceImpl,以便Spring容器知道它是一个Spring托管bean:

@Service
public class ItemsServiceImpl implements ItemsService {
    @Autowired
   private ItemsMapperCustom itemsMapperCustom;
    @Autowired
    private ItemsMapper itemsMapper;

   //other code
}