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

I wants to run this piece of logic as soon my application context get loaded but when i started my app it shows this error.

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

Can someone tell me how can I create a repository bean inside CommandLineRunner. I also googled it but could'nt find any answers.

This is my repository

@Repository
public interface EmployeeRepository extends 
ReactiveCouchbaseRepository<Employees, String>{ 
}
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Nikhil
  • 374
  • 1
  • 4
  • 21
  • Show whole ReactiveCouchbaseExample1Application.class – Antoniossss Dec 18 '18 at 20:06
  • Does this answer your question? ['Field required a bean of type that could not be found.' error spring restful API using mongodb](https://stackoverflow.com/questions/42907553/field-required-a-bean-of-type-that-could-not-be-found-error-spring-restful-ap) – Abhijit Sarkar Jul 02 '23 at 02:41
  • @Manuel Jordan 'asked Nov 6, 2018'. Pls do not edit dead posts :) – Artem Lisun Jul 02 '23 at 02:44

1 Answers1

1

Scanned component must be in the same package as @SpringBootApplication annotated class, or its subpackage. I can only assume it is ReactiveCouchbaseExample1Application.class.

Maybe your repo is in different package, or you didn't enable component scan with @SpringBootApplication or @ComponentScan.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • It’d be much better to ask clarifying questions than make assumptions, no matter how logical, as to what the problem might be. – Abhijit Sarkar Jul 02 '23 at 02:40
  • @AbhijitSarkar it's not an assumption - you could tell this is true by looking at his error – Artem Lisun Jul 02 '23 at 02:42
  • @ArtemLisun Not really. It is just as likely that the target class exists on the component scan path but isn’t a bean at all. That is, it doesn’t extend `Repository` or use one of bean markers. – Abhijit Sarkar Jul 02 '23 at 02:44
  • @AbhijitSarkar Low chance of that, but yea, you're right, it could be. – Artem Lisun Jul 02 '23 at 02:47
  • @ArtemLisun We aren’t here to play Bingo, thus my earlier comment. It’s unwise to answer a question using a bunch of assumptions as basis. – Abhijit Sarkar Jul 02 '23 at 02:50