如何为CrudRepository接口创建一个bean

时间:2016-09-20 15:27:04

标签: java spring spring-data

我正在尝试将数据库加载器添加到我的Spring App中,但是我收到以下错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [org...GameRepository] found
for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this 
dependency. Dependency annotations: {}"}}

我已经阅读了关于实例化存储库的内容,但我也看到了几个不需要的例子,那么我到底错过了什么?我是否需要一些配置类,我启用@EnableJpaRepositories

//GameRepository.java
@Repository
public interface GameRepository extends CrudRepository<Game, Long> {
}
//DatabaseLoader.java
@Component
public class DatabaseLoader implements CommandLineRunner{

    private final GameRepository repository;

    @Autowired
    public DatabaseLoader(GameRepository repository) {
        this.repository = repository;
    }

    @Override
    public void run(String... strings) throws Exception {
        this.repository.save(new Game("Sala 1"));
    }
//Application.java
@ComponentScan
@EnableWebMvc
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

1 个答案:

答案 0 :(得分:2)

您需要将@EnableJpaRepositories添加到您的配置