Enabling Spring @Async to Support Placeholders and SpEL Expressions
This article explains how to modify Spring's @Async annotation so that its value attribute can resolve placeholders or SpEL expressions, demonstrates a working demo, shows the required code changes using EmbeddedValueResolver, and shares debugging tricks and related Java language features.
The author discovered a Spring Framework issue requesting that the @Async annotation support placeholders or SpEL expressions (see the linked pull request) and decided to explore the problem in depth.
A minimal demo is built: a thread pool named why is defined, an @Async("why") method is created, a controller triggers the method via http://127.0.0.1:8085/insertUser?age=18 , and the application is started with @EnableAsync . The output confirms that the configuration works when a plain name is used.
When the value is written as an expression (e.g., @Async("${thread-pool.name}") ), Spring throws NoSuchBeanDefinitionException because the current implementation does not resolve the expression.
The proposed fix is to modify AsyncExecutionAspectSupport.findQualifiedExecutor so that, if the supplied BeanFactory is a ConfigurableBeanFactory , an EmbeddedValueResolver is used to resolve the qualifier. The key classes are ConfigurableBeanFactory (a full‑featured bean factory) and EmbeddedValueResolver (which resolves placeholders and expressions). The core change adds five lines that obtain an EmbeddedValueResolver and call resolveStringValue on the qualifier.
To experiment without rebuilding Spring, the author demonstrates a “quick‑hack” using the IDE’s Evaluate Expression (Alt+F8) feature. The following snippet can be pasted into the debugger to resolve the qualifier at runtime:
if (beanFactory instanceof ConfigurableBeanFactory) {
EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver((ConfigurableBeanFactory)beanFactory);
qualifier = embeddedValueResolver.resolveStringValue(qualifier);
}The technique instantly shows the resolved value in the logs, proving that the modification works without recompiling the framework.
The article also touches on Java’s newer instanceof pattern‑matching (available since JDK 14) and contrasts the old verbose style with the concise pattern‑matching syntax, highlighting readability and safety benefits.
Looking ahead, Spring 6 and Spring Boot 3 will adopt JDK 17 as the baseline, opening opportunities to contribute similar small improvements (e.g., updating instanceof usages). The author encourages readers to make incremental contributions to open‑source projects, noting that even a single line change can earn recognition, such as a coffee cup from the Dubbo community.
In summary, the piece shows how to enable expression support for @Async , provides a practical debugging shortcut, discusses related Java language features, and motivates developers to contribute modest yet valuable changes to large frameworks.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.