2017-01-10 20 views
-1

Я работаю с данными весны и весны JPA. При развертывании мое приложение в Tomcat Я получаю следующее исключение:Почему я получаю исключение NoSuchBeanDefinitionException при развертывании приложения Spring в 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 /> 
+0

Вы аннотировали класс реализации 'MyRepository' с' @ Repository'? – Arpit

+0

Добавьте аннотацию @Repository в ваш репозиторий и дайте ему уйти. Поскольку вы используете старую школьную программу для сканирования компонентов ваших репозиториев, вам нужно будет идентифицировать их с помощью Spring –

+0

@Arpit: OP использует Spring Data, который генерирует реализацию. Он не может аннотировать репозиторий. – dur

ответ

3

Я не видя ваше хранилище примеч. Это может быть причиной того, что Spring не смог создать компонент для MyRepository во время сканирования компонентов. Аннотировать его @Repository