CommandLineRunner run() method is not being called

Viewed 2410

I have tried all the answers provided on this platform but didn't work. I am executing this command line runner but the run method is not calling.

I appricaite for your help.

Thanks.

MyCommandlineRunner class

enter image description here

I have tried below solution but got this exception.

1st: at your Bootstrap class (the class which has the main method with SpringApplication.run(..., args)), you can add the attribute scanBasePackages at the @SpringBootApplication annotation:

@SpringBootApplication(scanBasePackages = {"com.project.data.runner"}) This will tell Spring to look for components, also, at the com.project.data.runner package.

enter image description here

1 Answers

Your Bootstrap class is on the package com.project.demo.data

Your command line runner is on the package com.project.data.runner

Spring will scan for components in the sub-packages of com.project.demo.data, that is, in com.project.demo.data.*, that's why your command line runner is never ran. He's never found by Spring.

You have some options, but I'll state two:

1st: at your Bootstrap class (the class which has the main method with SpringApplication.run(..., args)), you can add the attribute scanBasePackages at the @SpringBootApplication annotation:

@SpringBootApplication(scanBasePackages = {"com.project.data.runner"})

This will tell Spring to look for components, also, at the com.project.data.runner package.

OR

2nd: move your command line runner from com.project.data.runner to com.project.demo.data.runner (or something similar that is under com.project.demo.data)

UPDATE

As we've solved the problem in the chat, I'll update the answer with the solution.

Since you're using Spring Data JPA Repositories, if your repositories are not under the package or sub-packages where your Bootstrap class resides, then you must add @EnableJpaRepositories to a @Configuration class and explicitly configure on which package your repository is.

Your bootstrap class will look like this:

@SpringBootApplication(scanBasePackages = { "com.project.data.runner"})
@Configuration
@EntityScan("com.project.data.entity")
@EnableJpaRepositories(basePackages = { "com.project.data.repository" })
public class DataJpaApplication {

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