How to solve NoSuchBeanDefinitionException when developing multi-module springboot applications
When we develop multi-module springboot application,the layout is as follows:
1. The project layout
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:
3. Run the testcase
Now run the testcase, we got this exception
4. The reason
Because @SpringBootApplication only scan its base packages, the source code of @SpringBootApplication is as follows:
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
Now run test again, the exception disappeared.
You can find detail documents about the springboot here: