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

Viewed 29

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 @Profile annotation - regardless if active or inactive 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?

If I debug the code, than the debugger stops at the foo field before the CommandLineRunner is triggered. So I suspect it could be found but it is discarded to prevent a problem with inactive beans?

0 Answers
Related