IntelliJ cannot autowire the parameter in @ServiceActivator method if I use @EnableAutoConfiguration

Viewed 441

With auto configuration enabled, my Spring Integration service activator gives me an error in IntelliJ: "Could not autowire. No beans of 'String' type found."

If I disable auto configuration, the error goes away.

Can I exclude a class from auto configuration to "fix" this? How do I know which?

Here's the @ServiceActivator:

@Slf4j
@MessageEndpoint
public class StringProcessor {
  @ServiceActivator(inputChannel = "channel1")
  public void processString(String s) {
    log.info(s);
  }
}

IntelliJ error message

The error can be toggled by the @EnableAutoConfiguration (@SpringBootApplication) annotation:

//@SpringBootApplication
@ComponentScan
@Configuration
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

How do I figure out if this is an IntelliJ false alarm, a Spring Integration issue, or am I holding it wrong? ;o)

2 Answers

That's IntelliJ IDEA false alarm and wrong assumption at the same time.

The signature and structure of that @ServiceActivator was never designed for injections. Such a method is called at runtime when a message is appeared in that channel1. So, an endpoint activate that service method and passes a payload of message into an s argument of the method. With respective converting if necessary. The atuwiring assumption over there is a bug in the IDE.

See more about service activator in the docs: https://docs.spring.io/spring-integration/reference/html/messaging-endpoints.html#service-activator

It is a false alarm, and somebody has raised this as an issue with JetBrains

https://youtrack.jetbrains.com/issue/IDEA-264916

Would you mind clicking the thumbs up next to the title so Jetbrains knows that other people would like this issue fixed.

In the meantime you can disable the introspection at the method level with the following

@Slf4j
@MessageEndpoint
public class StringProcessor {

  @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
  @ServiceActivator(inputChannel = "channel1")
  public void processString(String s) {
    log.info(s);
  }

}

This type of warning is always a 'best guess' from IDEA, and as configs get more complicated, particular if you are refering to beans created in external jars it will happen more often.

They do not stop compilation or initial execution, but Spring itself will terminate execution if the warning is actually correct.

Related