2017-01-10 6 views
-1

私はSpringとSpringのデータJPAで作業しています。 Tomcatの中に自分のアプリケーションをデプロイするときに、私は次の例外を取得しています:TomcatでSpringアプリケーションをデプロイするとNoSuchBeanDefinitionExceptionが表示されるのはなぜですか?

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

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)} 

次は私のコードです

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

あなたは '@Rリポジトリ 'で' MyRepository'の実装クラスに注釈を付けましたか? – Arpit

+0

リポジトリに@Repositoryアノテーションを追加して、それを行ってください。古い学校でリポジトリをスキャンする手段を使用しているので、それらをSpringに識別する必要があります。 –

+0

@Arpit:OPはSpringデータを使用して実装を生成します。彼はリポジトリに注釈を付けることはできません。 – dur

答えて

3

私はあなたのリポジトリが注釈付きのを見ていませんよ。これは、コンポーネントスキャン中にSpringがMyRepositoryのBeanを作成できなかった理由です。それに注釈を付ける@Repository

関連する問題