3

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

2 Answers 2

8

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);
    }
}
13
  • Hi Matheus, I have tried 1st solution but got that exception. I have attached the screen shot.
    – Naresh
    Mar 28, 2021 at 5:31
  • And have tried 2nd one but haven't worked.
    – Naresh
    Mar 28, 2021 at 5:31
  • In which package is your PassangerRepository?
    – Matheus
    Mar 28, 2021 at 5:33
  • It is in "com.project.data.repository" package
    – Naresh
    Mar 28, 2021 at 5:35
  • 1
    Let's go to the chat, I've tested out and it worked properly here.
    – Matheus
    Mar 28, 2021 at 15:44
0

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.