While modularising our project into different independent maven projects using spring boot and maven, we have came across a issue where autowiring of beans in multi module maven project is not working.
Just to give you an overview of the issue, below are the independent maven projects developed so far
Below is the dependency structure RewardsApp-> Rewards -> DBservices1 -> Coreservices
The problem is @Autowired
annotation used in Rewards and DBservices1 to fetch the mapped services annotated with @Service
/@Repository
are not available in RewardsApp
Project.
As a workaround we have configured the beans in RewardsApp
with @Bean
annotation, then the services are available to the server to start successfully.
With this approach we need to manually configure all the beans in RewardsApp
used in dependent projects.
We have many services and repositories in our application and we think creating beans like this not a proper way as many beans need to be created.
Please note that we have created all the spring boot controllers,services,repositorys across all projects under
package com.company.application
Below is the snippet of main class:
@SpringBootApplication
@ComponentScan(basePackages = {"com.company.application"})
public class RewardsApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(RewardsApp.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RewardsApp.class);
}
}
/**
*Manual beans in RewardsApp
**/
@Bean
public SomeService someService()
{
return new SomeService();
}
By adding below annotation in RewardsApp.java did the trick for me, now autowiring was working for the classes inside the jars
@ComponentScan(basePackages = {"com.company"})
@EntityScan(basePackages = {"com.company"})
@EnableJpaRepositories(basePackages = {"com.company"})
I guess above are for Services,Entities(Domains),Repositories
How about having a configuration class (with relevant comp scans) for each module and importing those configs into your application class?
@SpringBootApplication
@ComponentScan(...)
@Import({RewardsContext.class, DBservicesContext.class})
...
Import docs here
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