Search code examples
springspring-bootgroovyclassloaderdynamic-class-creation

How to supply runtime generated groovy classes to Spring Boot context configuration process


I am generating classes from Groovy source and store them in GroovyClassLoader. These classes have @Configuration annotation. They are in package a.b.c.d.e. I want them to configure my spring context. I set thread context class loader to be this GroovyClassLoader.

My Spring Boot application is set up to scan package @ComponentScan("a.b.c"). I can confirm that Spring Boot is indeed using this GroovyClassLoader and that these generated classes can be fetched with this class loader.

The context gets initialized after the class generation is finished and classes are stored in GroovyClassLoader.

However, Spring Boot is oblivious of these classes. I suspect that component scan goes thru jars and directories where classes are stored, and these generated classes are not materialized in any of these places?

How should I supply these generated configuration classes to Spring Boot?


Solution

  • Since you already have generated classes before you start the Spring Boot context, you could register them by using SpringApplicationBuilder.sources(Class<?>...) method.