私は最初から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になります。そこに記載されている回答をコピーしようとしましたが、おそらく問題の原因となっているディレクトリ構造が異なります。
'GameServiceImpl'に' @ Component'を注釈する必要があるかもしれません。 –
'Games'クラスを' com.riisan.core'に移動して、 '@ SpringBootApplication'以外のすべてのアノテーションを削除してから、アプリケーションを再起動してください。これですべてが自動的に検出され、選択されます。 –