Reputation: 85
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
Upvotes: 0
Views: 663
Reputation: 187
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:
@EnableAutoConfiguration
: enable Spring Boot’s auto-configuration mechanism@ComponentScan
: enable @Component scan on the package where the application is located @Configuration
: allow to register extra beans in the context or import additional configuration classesFor further details, you can read here
Upvotes: 0
Reputation: 3170
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.
Upvotes: 1