When we develop multi-module springboot application,the layout is as follows:

1. The project layout

my-test-project
  +module1
    +--pom.xml
    +--src
      +--main
        +--com
          +--module1
            +--domain1
              +--Base1.java
            +--Application.java
  +module2
    +--pom.xml
    +--src
      +--main
        +--com
          +--module2
            +--Application.java
      +--test
        +--com
          +--module2
            +--MyTest.java

As you can see , module1 has a package named com.module1, and module2 has a package name com.module2, and module2 has a testcase named com.module2.MyTest

2. The testcase

Now in the module2’s testcase , I want to reference the module1’s class Base1.java, like this:

MyTest.java in module2:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
    @Autowired
    private com.module1.Base1 base1;

    @Test
    public void test() {

    }
}

3. Run the testcase

Now run the testcase, we got this exception

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.module1.Base1' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
  ... 33 more

4. The reason

Because @SpringBootApplication only scan its base packages, the source code of @SpringBootApplication is as follows:

@AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )

As the upper code shown, the springboot application only scan current package for spring beans, so when we try to reference other modules’ bean, it’s not been scanned yet. So we add this to the module2’s @SpringBootApplication

@SpringBootApplication
@ComponentScan("com.module2")
public class Application {
  //ignore other lines

} 

Now run test again, the exception disappeared.

You can find detail documents about the springboot here: