jeudi 24 février 2022

Adding a @Profile annotation to a component prevents ClassPathScanningCandidateComponentProvider from finding the bean

I have an annotation @LogProperty that can be applied to fields.

package com.foo;

@Component
@ConfigurationProperties("myapp")
public class FooClass {
    @LogProperty
    private String foo = "bar";
...

And some code that is looking for @ConfigurationProperties annotations:

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(ConfigurationProperties.class));

Set<BeanDefinition> components = scanner.findCandidateComponents("com.foo");
for (BeanDefinition beanDefinition : components) {
    ...
    check-for-LogProperty-annotation-and-do-something()
    ...

This code is then triggered in the run(String... args) method of a CommandLineRunner

With this I'm able to e.g. log the field/value to the console.

myapp: foo=bar

But if I add an arbitrary (e.g. @Profile("buuhh")) to the FooClass the scanning code from above does no longer find the annotated field.

Why does the @Profile annotation prevents it from being found? Is there a way to "fix" this?

I suspect there is a timing issue. But If I debug the code, than the debugger stops at the foo field before the CommandLineRunner is triggered.





Aucun commentaire:

Enregistrer un commentaire