1

We created a Spring starter library where we expose a configuration class. Below is the spring.factories and the configuration class

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sample.SampleRequestFilter

SampleRequestFilter.class

@Configuration
@AutoConfigureOrder(Integer.MIN_VALUE)
@ConditionalOnProperty({"sample.property"})
public class SampleRequestFilter {
    @Bean
    @ConditionalOnMissingBean
    public ExchangeFilterFunction sampleFilter(Blabla blabla) {
        return (clientRequest, nextFilter) -> {
            .... bla bla
            return nextFilter.exchange(clientRequest);
        };
    }
}

In my project attempting to use this starter library I have added the starter library as as a dependency implementation 'blablabla'. Everything works fine and my bean is generated as expected. However, IntelliJ complains about the SampleRequestFilter saying Could not autowire. No beans of 'ExchangeFilterFunction' type found. despite the fact that the application starts and correctly autowires a bean.

Did I miss anything in my understanding of how to create starter libraries? Or is this just an IntelliJ bug? If the latter is there anyway to suppress this without adding annotations to my code? I have upgraded to the latest version of IntelliJ and have tried invalidating caches.

andresmonc
  • 398
  • 7
  • 21

2 Answers2

1

The root cause for your problem could be, that your bean is created after the beans that depend on it. This might be the case when a bean you create in your starter as auto configuration is created after the Spring auto configuration that depends on it as the Spring auto configuration is created at an earlier point during application initialization.
You can solve this by telling Spring that your auto configuration has to be applied before some other auto configuration by using the @AutoConfigureBefore annotation:

@Configuration
@AutoConfigureBefore(WebClientAutoConfiguration.class) // <- this should do the trick
@ConditionalOnProperty({"sample.property"})
public class SampleRequestFilter {
    @Bean
    @ConditionalOnMissingBean
    public ExchangeFilterFunction sampleFilter(Blabla blabla) {
        return (clientRequest, nextFilter) -> {
            .... bla bla
            return nextFilter.exchange(clientRequest);
        };
    }
}

My guess is that you need the WebClientAutoConfiguration but without knowing your code I can't say for sure, so you might have to try different auto configurations as well. But as the ExchangeFilterFunction resides in the org.springframework.web.reactive.function.client package and the WebClientAutoConfiguration in the org.springframework.boot.autoconfigure.web.reactive.function.client package it might be a fit.

times29
  • 2,782
  • 2
  • 21
  • 40
  • I do like this @AutoConfigureBefore(WebClientAutoConfiguration.class) much better than what I had before. Unfortunately, this still doesn't resolve the issue :( but +1 for the improvement – andresmonc Jan 06 '23 at 20:17
  • Hi @andresmonc, could you post the full stack trace? In which class do you attempt to autowire the bean that can't be found? Could you share that code as well? – times29 Jan 07 '23 at 08:06
  • This is purely an intellij issue. Code runs perfectly so there's not really a stacktrace – andresmonc Jan 07 '23 at 14:54
  • 1
    Ah, I didn’t realize that. Could you maybe append a screenshot of the error? – times29 Jan 08 '23 at 08:30
0

Well, I finally figured it out. If you too are facing this issue then add the following dependencies to your project:

    annotationProcessor 'org.springframework.boot:spring-boot-autoconfigure-processor'

I assume IntelliJ just scans the generated metadata to look for beans.

andresmonc
  • 398
  • 7
  • 21