Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CommandLineRunner run() method is not being called

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

like image 450
Naresh Avatar asked Aug 31 '26 03:08

Naresh


2 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);
    }
}
like image 114
Matheus Cirillo Avatar answered Sep 02 '26 19:09

Matheus Cirillo


I had the same issue and the fix was to add @Component to the class definition.

like image 39
Richard Keene Avatar answered Sep 02 '26 18:09

Richard Keene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!