使用命令行运行器在Spring启动应用程序中创建Bean

问题描述 投票:0回答:1

@SpringBootApplication public class ReactiveCouchbaseExample1Application {

@Bean
CommandLineRunner employees(ApplicationContext context) {
    EmployeeRepository repository = context.getBean(EmployeeRepository.class);
    return args -> {
        repository
            .deleteAll()
            .subscribe(null,null,()->{
                Stream.of(new Employees(UUID.randomUUID().toString(), "Nikhil", 23, 3000L),
                        new Employees(UUID.randomUUID().toString(), "Shubham", 23, 3000L),
                        new Employees(UUID.randomUUID().toString(), "Anshul", 23, 3000L))
                .forEach(employee->{
                    repository.save(employee)
                    .subscribe(System.out::println);
                });
            });
    };
}

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

我希望在我的应用程序上下文加载后运行这条逻辑但是当我启动我的应用程序时它会显示此错误。

Method employees in com.reactive.reactivecouchbaseexample1.ReactiveCouchbaseExample1Application required a bean of type 'com.reactive.repository.EmployeeRepository' that could not be found.

有人能告诉我如何在CommandLineRunner中创建一个存储库bean。我也搜索了它,但无法找到任何答案。

这是我的存储库

@Repository
public interface EmployeeRepository extends 
ReactiveCouchbaseRepository<Employees, String>{ 
}
spring spring-data
1个回答
0
投票

扫描的组件必须与@SpringBootApplication带注释的类或其子包在同一个包中。我只能假设它是ReactiveCouchbaseExample1Application.class

也许您的仓库在不同的包装中,或者您没有使用@SpringBootApplication@ComponentScan启用组件扫描。

© www.soinside.com 2019 - 2024. All rights reserved.