1

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?

igobivo
  • 433
  • 1
  • 4
  • 17
  • 1
    Just a question for you - if it is runtime generated, then why not just register the bean directly with the ApplicationContext instead of fighting with the timing/class load order? – pczeus Apr 28 '20 at 01:53
  • @pczeus, the beans and configurations (some of them) are defined as Groovy classes which are supplied in the form of source code after the service is deployed. Upon starting, sources are being read and compiled into classes, and only then `SpringApplication#run` is called. So this should take care of the timing issues. – igobivo Apr 28 '20 at 07:25

1 Answers1

0

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

igobivo
  • 433
  • 1
  • 4
  • 17