没有为 @Autowired 服务找到依赖类型的限定 Bean
Posted
技术标签:
【中文标题】没有为 @Autowired 服务找到依赖类型的限定 Bean【英文标题】:No Qualifying Bean of type found for dependency for @Autowired service 【发布时间】: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
... 然后重新启动您的应用程序。现在一切都会被自动检测并拾取。
【参考方案1】:
从 RiisanConfig 中移除除 @SpringBootApplication 之外的所有注解。
【讨论】:
【参考方案2】:问题是@ComponentScan
没有指定基础包,来自doc:
如果未定义特定的包,则从声明此注解的类的包中进行扫描。
(重点是我的)
注意,同样适用于:@EnableJpaRepositories
【讨论】:
当我添加@ComponentScan(basePackages="com.riisan.core")
或列出任意数量的包(包括所有这些包)时,所有自动装配都会失败。
编辑您的问题,并发布您在“所有自动连线失败”时获得的堆栈跟踪
原来我还需要@EntityScan
来避免Not an managed type
问题。谢谢!【参考方案3】:
在RiisanConfig
中尝试@ComponentScan("com.riisan.core.service.impl")
。 Spring默认扫描子包,但com.riisan.core.service.impl不是com.riisan.core.config的子包
另外,List<Game> findAll()
;在GameRepository
中是不必要的,因为CrudRepository
(JpaRepository
的超接口)已经包含findAll()
。 (JpaRepository
还添加了一个带有 Paging 参数的findAll
。)
【讨论】:
以上是关于没有为 @Autowired 服务找到依赖类型的限定 Bean的主要内容,如果未能解决你的问题,请参考以下文章
没有找到能够从类型 [java.lang.String] 转换为类型 [@Autowired @ManyToOne @JoinColumn com.papertrue.country.Country]