Search code examples
javaspringspring-bootspring-restcontrollerspring-context

Spring-Boot Components from other jar are not in context


I have following situation. JDK 17, Spring-Boot: 2.6.2. A gradle multi-project. One project is a library (java-library, no spring boot plugin). Another project is a spring boot application with spring boot plugin. Generally, the spring dependency management plugin is not used, gradle platform concept is used instead. Application project includes dependency to library project per "implementation(project(':.."

Library:

  • Library project and spring boot application project have different packages.

like: library root package is a.b.c and application root package is a.b.d

  • Library project has in root package of the package hierarchy a configuration class (for example a.b.c.LibraryConfig) which is annotated with @Configuration annotation and with @ComponentScan annotation, which has attribute "basePackageClass" pointing to this configuration class:
    @Configuration
    @ComponentScan(basePackageClass = LibraryConfig.class)
    public class LibraryConfig {
  • Inside of library hierarchy (so in packages a.b.c.*) are services and rest controllers.

Application:

  • Application has in root package of the package hierarchy (for example in a.b.d) application class and in sub-package "config" it has configuration class (would be for example a.b.d.config.AppConfig), which imports the configuration class from library:
  @Configuration
  @Import(a.b.c.LibraryConfig.class)
  public class AppConfig {

Problem: Classes from the library are there in runtime - we are able to load them. No any component of the library is in context (not registered), neither services nor rest controllers.

What are we missing?

We have tried different constellations of imports and scans. Also added component scan to application class and set there particular packages. Nothing helped.


Solution

  • The problem was, that the main application was using spring boot indexing annotation processor and library - not. So after enabling the annotation processor for library problem was solved.