2016-04-20 3 views
0

Ich bin von Grund auf meiner ersten Frühlings Boot-Anwendung arbeiten und Online den Stil anderer Beispiele usw. gefunden imitiert wurden, ich habe die folgende Verzeichnisstruktur:Keine Qualifikation Bean vom Typ für Abhängigkeit für @Autowired Dienst gefunden

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

Innerhalb der Hauptkonfigurationsdatei, die ich habe:

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

ich habe das Spiel und der Spieler mit @Entity markiert, und das Repository wie folgt aussieht:

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

Meine Ressourcenkomponente ist:

@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(); 
    } 
} 

Und schließlich, der Service und die Service-Implementierung sind wie folgt:

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

Alles funktioniert, um die GET Anfrage zu schaffen. Auf die GET-Anforderung empfängt, erhalte ich eine Fehlermeldung:

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. 

Ich habe versucht, alle Schritte in anderen Beiträgen SE, wie @Service-@Service("gameService") Ändern oder Hinzufügen @Qualifier, aber ohne Erfolg. Diese Struktur und alle Annotationen spiegeln die einer funktionierenden Anwendung wider, die ich als Basis für den Versuch, diese Anwendung einzurichten, verwendet habe, mit der geringfügigen Optimierung, dass die funktionierende Anwendung ein MongoRepository anstelle von JpaRepository verwendet. Was verursacht diesen Fehler?

UPDATE: unten einige der Antworten Versuch habe ich versucht:

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

Diese in den folgenden Fehlern führt bei Start der Anwendung:

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

ich das Paket zusätzlich versucht, die Auflistung für die Repository in `@EnableJpaRepositories (" com.riisan.core.repsiony "), das zu Not an managed type führt. Ich habe versucht, die dort aufgelistete Antwort zu kopieren, aber die Verzeichnisstruktur ist anders, was wahrscheinlich das Problem verursacht.

+0

Möglicherweise müssen Sie GameServiceImpl mit '@ Component' annotieren. –

+0

Verschieben Sie Ihre 'Games' Klasse in' com.riisan.core' Entfernen Sie alle Anmerkungen außer '@ SpringBootApplication' ... Starten Sie dann Ihre Anwendung neu. Jetzt wird alles automatisch erkannt und abgeholt. –

Antwort

2

Das Problem ist @ComponentScan ohne eine Basispaket spezifiziert, aus der doc:

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

(Schwerpunkt ist Mine)


Note, die gleiche für geht: @EnableJpaRepositories

+0

Wenn ich den '@ComponentScan (basePackages = {" com.riisan.core "})' hinzufüge, oder eine beliebige Anzahl der Pakete auflisten, einschließlich aller von ihnen, scheitern alle Autowire. – Teofrostus

+0

Editiere deine Frage und poste den Stacktrace, den du bekommst, wenn "alle Autowire fehlschlagen" –

+0

Es stellte sich heraus, dass ich auch '@ EntityScan' benötige, um das' Not a managed type' Problem zu vermeiden. Vielen Dank! – Teofrostus

1

versuchen Sie @ComponentScan("com.riisan.core.service.impl") in RiisanConfig. Spring scannt standardmäßig Unterpakete, aber com.riisan.core.service.impl ist kein Unterpaket von com.riisan.core.config

Auch List<Game> findAll(); in GameRepository ist unnötig, da CrudRepository (eine Superschnittstelle von JpaRepository) bereits eine findAll() enthält. (JpaRepository fügt zusätzlich eine findAll hinzu, die ein Paging-Argument verwendet.)

0

Entfernen Sie alle Anmerkungen aus RiisanConfig, aber @SpringBootApplication.

Verwandte Themen