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.


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.

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);
}
}
I had the same issue and the fix was to add @Component to the class definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With