Skip to content

Spring MVC: @EnableAsync needs to be redeclared for each ApplicationContext #34843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smfukaya opened this issue Apr 29, 2025 · 0 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@smfukaya
Copy link

I'm using spring-context-6.2.2.

Similar to SPR-16852, @EnableAsync must be redeclared for each ServletRegistrationBean because the AsyncAnnotationBeanPostProcessor registered in the root ApplicationContext is not inherited by the child ApplicationContext and there can only be one AsyncConfigurer.

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    private static final String DISPATCHER_SERVLET_MAPPINGS_FORMAT = "/%s/*";
    @Bean
    ServletRegistrationBean<DispatcherServlet> api() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        AnnotationConfigWebApplicationContext applicationContext =
        		new AnnotationConfigWebApplicationContext();
        applicationContext.register(ApiConfig.class);
        dispatcherServlet.setApplicationContext(applicationContext);
        ServletRegistrationBean<DispatcherServlet> servletRegistrationBean =
        		new ServletRegistrationBean<>(
        				dispatcherServlet,
        				String.format(DISPATCHER_SERVLET_MAPPINGS_FORMAT, ApiConfig.API_SERVLET_NAME
        						)
        				);
        servletRegistrationBean.setName(ApiConfig.API_SERVLET_NAME);
        return servletRegistrationBean;
    }
}
@Configuration
@EnableAsync

@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableScheduling
@ComponentScan(basePackages = { "..." }
		, includeFilters = @Filter(Controller.class))
public class ApiConfig {
	public static final String API_SERVLET_NAME = "api";

	public ApiConfig() {

	}
}

I suggest adding a note to @EnableAsync Javadoc akin to what is provided by @EnableScheduling:

* <p><b>Note: {@code @EnableScheduling} applies to its local application context only,
* allowing for selective scheduling of beans at different levels. Please redeclare
* {@code @EnableScheduling} in each individual context, for example, the common root web
* application context and any separate {@code DispatcherServlet} application contexts,
* if you need to apply its behavior at multiple levels.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 29, 2025
@jhoeller jhoeller added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 30, 2025
@jhoeller jhoeller self-assigned this Apr 30, 2025
@jhoeller jhoeller added this to the 6.2.7 milestone Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants