为什么我在Tomcat中部署Spring应用程序时会收到NoSuchBeanDefinitionException?

时间:2017-01-10 17:45:57

标签: spring spring-data-jpa autowired spring-4

我正在使用Spring和Spring Data JPA。在Tomcat中部署我的应用程序时,我得到以下异常:

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 com.service.MyService com.controller.MyController.myService; nested exception 
 is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'MyService': Injection of autowired dependencies failed; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
 private com.repository.MyRepository com.service.MyService.myRepository; nested exception is 
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 
 [com.repository.MyRepository] 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)}

以下是我的代码:

MyController.java

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.domain.MyEntity;
import com.service.MyService;

@RestController
@RequestMapping(MyController.ROOT_RESOURCE_PATH)
public class MyController{

    public static final String ROOT_RESOURCE_PATH = "/test";

    @Autowired
    private MyService myService;

    @RequestMapping(value="/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<MyEntity> getAll() {
        return myService.getAll();
    }
}

MyService.java

package com.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.domain.MyEntity;
import com.repository.MyRepository;

@Service(value = "MyService")
public class MyService {

    @Autowired
    private MyRepository myRepository;

    public List<MyEntity> getAll() {
        return myRepository.findAll();
    }
}

MyRepository.java

package com.repository;

import java.util.List;

import org.springframework.data.repository.Repository;

import com.domain.MyEntity;

public interface MyRepository extends Repository<MyEntity, Long> {

    public List<MyEntity> findAll();
}

}

所有MyApplication-context.xml中

<jpa:repositories base-package="com.repository" /> 

<context:component-scan base-package="com.service" />

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

<context:annotation-config />

1 个答案:

答案 0 :(得分:3)

我没有看到您的存储库已注释。这可能是Spring在组件扫描期间无法为MyRepository创建bean的原因。使用@Repository

对其进行注释