【发布时间】:2016-08-12 14:23:16
【问题描述】:
我正在从头开始开发我的第一个 Spring Boot 应用程序,并且一直在模仿网上其他示例的样式等。我有以下目录结构:
|com.riisan.core
|\
||config
||\
|||RiisanConfig
|||JerseyConfig
||domain
||\
|||Game
|||Player
||repository
||\
|||GameRespository
||resources
||\
|||Games
||service
||\
|||impl
|||\
||||GameServiceImpl
|||GameService
在主配置文件中,我有:
@Configuration
@SpringBootApplication
@ComponentScan
@EnableJpaRepositories
public class RiisanConfig {
public static void main(String args[]){
SpringApplication.run(RiisanConfig.class, args);
}
}
我已经用@Entity 标记了游戏和播放器,并且存储库看起来像:
@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.
我已经尝试了其他 SE 帖子中的所有步骤,例如将 @Service 更改为 @Service("gameService") 或添加 @Qualifier,但无济于事。这种结构和所有注释都反映了我用作尝试设置此应用程序的基础的工作应用程序的结构,并稍微调整了工作应用程序使用的是 MongoRepository 而不是 JpaRepository。是什么导致了这个错误?
更新: 尝试了以下一些答案,我尝试了:
@Configuration
@SpringBootApplication
@ComponentScan(basePackages = "com.riisan.core")
@EnableJpaRepositories
public class RiisanConfig {
public static void main(String args[]){
SpringApplication.run(RiisanConfig.class, args);
}
}
这会在启动应用程序时导致以下错误:
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)}
我还尝试在`@EnableJpaRepositories("com.riisan.core.repsitory") 中列出存储库的包,这导致Not an managed type。我尝试复制那里列出的答案,但目录结构不同,这可能是导致问题的原因。
【问题讨论】:
-
可能你需要用
@Component注释GameServiceImpl。 -
将您的
Games类移动到com.riisan.core删除所有注释,但@SpringBootApplication... 然后重新启动您的应用程序。现在一切都会被自动检测并拾取。