I have a library, that provides an ability to register user-defined context objects inside of my overarching application context. The library is written in plain Java so that it can be used in both Spring and Quarkus projects.
At the moment i’m evaluating benefits of Native compilation for both frameworks and this library is a roadblock for Spring.
The idea is to annotate Contexts with a custom annotation, then look for those classes using Reflections and register them within the library’s registry for easy access
A minimal sample would be something like this:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RegisterProvider {
}
@RegisterProvider
public class AnnotatedClass {
public AnnotatedClass(){}
}
@SpringBootApplication
@RestController
public class DemoApplication {
private static final Reflections ref = new Reflections("com.example.demo", Scanners.FieldsAnnotated, Scanners.TypesAnnotated);
static {
for (Class<?> clz : ref.getTypesAnnotatedWith(RegisterProvider.class)) {
System.out.println("found: " + clz);
}
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/")
String home() {
return "Hello World!";
}
}
The code above works fine when i run it in vanilla mode, finding the AnnotatedClass
, however it fails to do so when compiled into a native executable.
I’ve tried the usual routine of registering it with @RegisterReflectionForBinding(AnnotatedClass.class)
, adding com.example.demo.AnnotatedClass
to reflect-config.json
and registering this class at build time using --initialize-at-build-time
, but to no avail.
Stuff like @PostConstruct
fires a bit too late for our purpose, unfortunately, we’d want this code to execute before beans will be initialized.
This same code works in Quarkus without an issue, which, i assume, can be attributed to them using Jandex.
Is there anything in Spring Native that would allow me to run this bit of code?