2016-04-20 8 views
0

私は最初からSpring Bootアプリケーションを開発しており、オンラインなどで見つかった他の例のスタイルを模倣しています。@Autowiredサービスへの依存のために見つかったタイプのBeanがありません

@Configuration 
@SpringBootApplication 
@ComponentScan 
@EnableJpaRepositories 
public class RiisanConfig { 
    public static void main(String args[]){ 
     SpringApplication.run(RiisanConfig.class, args); 
    } 
} 

私は@Entityとの両方のゲームとプレイヤーをマークした、とリポジトリは、次のようになります:メイン設定ファイル内

|com.riisan.core 
|\ 
||config 
||\ 
|||RiisanConfig 
|||JerseyConfig 
||domain 
||\ 
|||Game 
|||Player 
||repository 
||\ 
|||GameRespository 
||resources 
||\ 
|||Games 
||service 
||\ 
|||impl 
|||\ 
||||GameServiceImpl 
|||GameService 

、私が持っている

@Repository 
public interface GameRepository extends JpaRepository<Game, String> { 
    List<Game> findAll(); 
} 

私のリソースコンポーネントは次のとおりです。

@Component 
@Path("/games") 
@Api(value = "/games", description = "Games REST") 
public class Games { 
    @Autowired 
    GameService gameService; 

    @GET 
    @ApiOperation(value = "Get all Games", response = Game.class, responseContainer = "List") 
    @Produces(MediaType.APPLICATION_JSON) 
    public List<Game> getAllGames(){ 
     return gameService.getAllGames(); 
    } 
} 

そして最後に、サービスとサービスの実装は次のとおりです。

public interface GameService { 
    public List<Game> getAllGames(); 

    public Game saveGame(Game game); 
} 

@Service 
public class GameServiceImpl implements GameService { 
    @Autowired 
    GameRepository gameRepository; 

    public List<Game> getAllGames() { 
     return gameRepository.findAll(); 
    } 

    public Game saveGame(Game game) { 
     return null; 
    } 
} 

すべてがGETリクエストを作成するまで動作します。 GETリクエストを受信すると、私はエラーが表示されます。

No qualifying bean of type [com.riisan.core.service.GameService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 

私は、このような@Service("gameService")@Serviceを変更したり@Qualifierを追加するなど、他のSEの記事、のすべてのステップを試してみましたが、無駄にしました。この構造体とすべての注釈は、私がこのアプリケーションをセットアップしようとしたときのベースとして使っていた作業用アプリケーションのものです.JpaRepositoryの代わりにMongoRepositoryを使用しています。このエラーの原因は何ですか?

UPDATE: 下の回答のいくつかをしようと、私が試した:私は、さらに用のパッケージをリストアップしようとした

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'games': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.riisan.core.service.GameService com.riisan.core.resources.Games.gameService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gameServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.riisan.core.repository.GameRepository com.riisan.core.service.impl.GameServiceImpl.gameRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.riisan.core.repository.GameRepository] 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)} 
    at ... ... 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.riisan.core.service.GameService com.riisan.core.resources.Games.gameService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gameServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.riisan.core.repository.GameRepository com.riisan.core.service.impl.GameServiceImpl.gameRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.riisan.core.repository.GameRepository] 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)} 
    at ... ... 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gameServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.riisan.core.repository.GameRepository com.riisan.core.service.impl.GameServiceImpl.gameRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.riisan.core.repository.GameRepository] 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)} 
    at ... ... 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.riisan.core.repository.GameRepository com.riisan.core.service.impl.GameServiceImpl.gameRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.riisan.core.repository.GameRepository] 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)} 
    at ... ... 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.riisan.core.repository.GameRepository] 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)} 

これは、アプリケーションの起動時に次のエラーになり
@Configuration 
@SpringBootApplication 
@ComponentScan(basePackages = "com.riisan.core") 
@EnableJpaRepositories 
public class RiisanConfig { 
    public static void main(String args[]){ 
     SpringApplication.run(RiisanConfig.class, args); 
    } 
} 

`@EnableJpaRepositories(" com.riisan.core.repsitory ")のリポジトリで、Not an managed typeになります。そこに記載されている回答をコピーしようとしましたが、おそらく問題の原因となっているディレクトリ構造が異なります。

+0

'GameServiceImpl'に' @ Component'を注釈する必要があるかもしれません。 –

+0

'Games'クラスを' com.riisan.core'に移動して、 '@ SpringBootApplication'以外のすべてのアノテーションを削除してから、アプリケーションを再起動してください。これですべてが自動的に検出され、選択されます。 –

答えて

2

問題がdocから特定基本パッケージ、無し@ComponentScanある:同じについても

If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

(強調は私です)


注:@EnableJpaRepositories

+0

'@ComponentScan(basePackages = {" com.riisan.core "})'を追加するか、すべてのパッケージを含めて任意の数のパッケージをリストすると、すべての自動配線が失敗します。 – Teofrostus

+0

あなたの質問を編集し、 "すべてのautowiresが失敗する"ときに得たstacktraceを投稿してください。 –

+0

「マネージタイプではない」問題を避けるために '@ EntityScan'も必要でした。ありがとう! – Teofrostus

1

@ComponentScan("com.riisan.core.service.impl")RiisanConfigに試してください。 Springはデフォルトでサブパッケージをスキャンしますが、com.riisan.core.service.implはcom.riisan.core.configのサブパッケージではありません。

また、List<Game> findAll(); には、が既に含まれています。(スーパーインターフェイスはJpaRepository)です。 (JpaRepositoryには、さらに、ページング引数をとるfindAllが追加されます。)

0

@SpringBootApplication以外のRiisanConfigからすべての注釈を削除します。

関連する問題