Description
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.