60 lines
2.0 KiB
Plaintext
60 lines
2.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
`@Configuration` is a class-level annotation indicating that an object is a source of bean definitions.
|
|
`@Configuration` classes declare beans through `@Bean`-annotated methods.
|
|
Calls to `@Bean` methods on `@Configuration` classes can also be used to define inter-bean dependencies.
|
|
The `@Bean` annotation indicates that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container.
|
|
|
|
Annotating a method of a bean with `@Async` will make it execute in a separate thread.
|
|
In other words, the caller will not wait for the completion of the called method.
|
|
|
|
The `@Async` annotation is not supported on methods declared within a `@Configuration` class.
|
|
This is because `@Async` methods are typically used for asynchronous processing, and they require certain infrastructure to be set up, which may not be available or appropriate in a `@Configuration` class.
|
|
|
|
== How to fix it
|
|
|
|
Don't use `@Async` annotations on methods of `@Configuration` classes.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
@EnableAsync
|
|
@Configuration
|
|
public class MyConfiguration {
|
|
|
|
@Async // Noncompliant - This is not allowed
|
|
public void asyncMethod() {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
@EnableAsync
|
|
@Configuration
|
|
public class MyConfiguration {
|
|
|
|
public void method() {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Async.html[Spring Framework - @Async]
|
|
* https://docs.spring.io/spring-framework/reference/core/beans/java/configuration-annotation.html[Spring Framework - Using the @Configuration annotation]
|
|
* https://docs.spring.io/spring-framework/reference/core/beans/java/basic-concepts.html[Spring Framework - Basic Concepts: @Bean and @Configuration]
|
|
|
|
=== Articles & blog posts
|
|
|
|
* https://www.baeldung.com/spring-async[Baeldung - How To Do @Async in Spring]
|