我正在尝试将数据库加载器添加到我的Spring应用程序,但是出现以下错误:

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

最佳答案

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

08-18 05:31