简体   繁体   中英

No Qualifying Bean of type found for dependency for @Autowired service

I'm working on my first Spring Boot application from scratch and have been mimicking the style of other examples found online etc. I have the following directory structure:

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

Within the main config file, I have:

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

I've marked both Game and Player with @Entity , and the repository looks like:

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

My resource component is:

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

And finally, the service and service implementation are as follows:

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

Everything works up to creating the GET request. Upon receiving the GET request, I receive an error:

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.

I've tried all the steps in other SE posts, such as changing @Service to @Service("gameService") or adding @Qualifier , but to no avail. This structure and all the annotations mirror that of a working application I used as a base for trying to set up this application, with the slight tweak that the working application is using a MongoRepository instead of JpaRepository. What is causing this error?

UPDATE: Attempting some of the answers below, I tried:

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

This results in the following errors upon launching the application:

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

I additionally tried listing the package for the repository in `@EnableJpaRepositories("com.riisan.core.repsitory"), which leads to Not an managed type . I tried copying the answer listed there, but the directory structure is different which is probably causing the problem.

The problem is @ComponentScan without a specifying a base package, from the doc :

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

(emphasis is mine)


Note, the same goes for: @EnableJpaRepositories

try @ComponentScan("com.riisan.core.service.impl") in RiisanConfig . Spring by default scans sub-packages, but com.riisan.core.service.impl is not a subpackage of com.riisan.core.config

Also, List<Game> findAll() ; in GameRepository is unnecessary, as CrudRepository (a super-interface of JpaRepository ) already includes a findAll() . ( JpaRepository additionally adds a findAll that takes a Paging argument.)

从RiisanConfig中删除所有注释,但删除@SpringBootApplication。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM