spring boot @Autowired a bean from another module

hamou amroun picture hamou amroun · Apr 26, 2018 · Viewed 8.3k times · Source

My question is how can i add a package to my list of component to scan @ComponentScan(basePackages = {"io.swagger", "com.company.project", like add it here }), but this package is in another module in my project,

here is the structure of my project :

springbootProject (maven project)/

  module1(mavenProject, com.company.module1)
       pom1.xml

  module2(mavenProject, com.company.module2)
       pom2.xml

pom.xml

in module 2 i have my main (@SpringbootAplication) where i want to @Autowired myRepository witch is in module 1

so how can i add the path

Answer

Magd Kudama picture Magd Kudama · Apr 26, 2018

Import ModuleB on ModuleA, and you'll be able to use it.

Project
|__ Module A (com.test.a)
|__ Module B (com.test.b)

In pom.xml on ModuleA, add:

<dependency>
  <groupId>com.test</groupId>
  <artifactId>b</artifactId>
  <version>1.0</version>
</dependency>

Then you should be able to add:

@ComponentScan(basePackages = {"com.test.b"})