我正在从头开始研究我的第一个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。我试着复制那里列出的答案,但目录结构不同,这可能导致问题。
答案 0 :(得分:2)
问题是@ComponentScan
,而没有指定基础包,来自doc:
如果未定义特定包,则将从声明此批注的类的包中进行扫描。
(重点是我的)
请注意,同样适用于:@EnableJpaRepositories
答案 1 :(得分:1)
在@ComponentScan("com.riisan.core.service.impl")
中尝试RiisanConfig
。 Spring默认扫描子包,但com.riisan.core.service.impl不是com.riisan.core.config的子包
此外,List<Game> findAll()
;因为GameRepository
(CrudRepository
的超级界面)已经包含JpaRepository
,所以findAll()
中不需要JpaRepository
。 (findAll
另外添加了一个带有Paging参数的<div data-dm-name label="First Name:" info="first_name"></div>
<div data-dm-name label="Last Name:" info="last_name"></div>
。)
答案 2 :(得分:0)
从RiisanConfig删除所有注释,但@SpringBootApplication。