Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Autowiring of beans is not working in maven multi module project

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

  1. Coreservices – Contains spring boot domain objects of whole application : Output JAR
  2. DBservices1-Contains spring boot repositories and services(Database Services) to access database : Output JAR
  3. Rewards -Contains Rewards module related files(Controllers, services(Business Logic Services), Views) : Output JAR
  4. RewardsApp- Independent deployable maven project : Output WAR

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();
}
like image 807
Aniketh Avatar asked Oct 13 '17 14:10

Aniketh


Video Answer


2 Answers

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

like image 131
Aniketh Avatar answered Oct 29 '22 05:10

Aniketh


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

like image 23
pappy-o Avatar answered Oct 29 '22 05:10

pappy-o