0

I have the below Main App:-

Both packages are in different module and i have "com.app.api is included in the pom.xml of com.app.batch

    //commented  @SpringBootApplication(scanBasePackages={"com.app.batch", "com.app.api"})
        public class App 
        {
            public static void main( String[] args )
            {

                 SpringApplication.run(App.class, args);
            }
        }

In com.app.api i have class ApiClass

@Service
public class ApiClass {}

in `com.app.batch i have

@Component
public class JobRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // TODO Auto-generated method stub
        apiClass.getData(1111);
    }
}

When i comment @SpringBootApplication(scanBasePackages={"com.app.batch", "com.app.api"}) i get the following error

Field apiClass in com.app.batch.config.JobRunner required a bean of type 'com.com.api.ApiClass' that could not be found.

How can i resolve the issue without using scanBasePackages .I don't want to use scanBasePackages as the module can get added in future and it can get cumberson

2
  • You need to only remove scanBasePackages={"com.app.batch", "com.app.api"} instead of whole @SpringBootApplication(...). Keep @SpringBootApplication and try Commented Jul 4, 2019 at 8:22
  • 1
    Your App class needs to be in com.app - which will automatically act like a base package and all the packages inside will be scanned by SpringBootApplication annotation. If you do not wish to change the package of App class then scanBasePackages is one of the better ways to do it. But I would suggest if possible change the package of App class. Commented Jul 4, 2019 at 8:29

2 Answers 2

1

If your not interested to use

@SpringBootApplication(scanBasePackages={"com.app.batch", "com.app.api"})

you need to change the package hierarchy so that spring scans the beans easily.

Your main SpringBootApplication class should be in com.app package and remaining classes should be in sub-packages. Like com.app.batch and com.app.api are sub-package of com.app

By using this kinda package hierarchy you no need scanBasePackages.

Sign up to request clarification or add additional context in comments.

Comments

0

What is the package of the App class?

It needs to be in the base package so that Spring Boot Application scans all the packages inside it.

@SpringBootApplication annotation enables the following annotations/features on its own:

  1. @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
  2. @ComponentScan: enable @Component scan on the package where the application is located
  3. @Configuration: allow to register extra beans in the context or import additional configuration classes

For further details, you can read here

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.