【问题标题】:Creating Bean inside Spring boot application using command line runner使用命令行运行器在 Spring Boot 应用程序中创建 Bean
【发布时间】:2019-04-09 18:39:27
【问题描述】:

@SpringBootApplication 公共类 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>{ 
}

【问题讨论】:

  • 显示整个 ReactiveCouchbaseExample1Application.class

标签: spring spring-data


【解决方案1】:

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

也许您的 repo 位于不同的包中,或者您没有使用 @SpringBootApplication@ComponentScan 启用组件扫描。

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 2017-01-16
    • 1970-01-01
    • 2017-10-08
    • 2021-06-03
    • 2020-05-18
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多