spring - 在 JBoss 上部署战争时,@Async 注释不起作用
问题描述
我有一个包含作为 Spring Boot 应用程序(1.5.18.RELEASE 版本)实现的 API Rest 的应用程序
此 API 包含一个异步执行服务方法的控制器。该方法标有@Async
注解
@EnableAsync
注释是在我的配置类上设置的。
当我像典型的 Spring Boot 应用程序一样执行应用程序时,该方法是异步执行的。如果我生成一个战争(使用 maven)并且这个战争部署在 JBoss(6.4 版本)上,相同的服务将同步执行。
有人可以向我解释这种行为吗?我应该添加任何类型的配置吗?
源代码如下:
Spring Boot 配置
@SpringBootApplication
@EnableCustomConfiguration
@EnableCaching
@EnableScheduling
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
我的自定义注释:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({CustomServicesConfiguration.class})
@Documented
public @interface EnableCustomConfiguration {
}
我的配置类:
@Configuration
@ComponentScan("com.bs.custom.api")
@EntityScan(basePackages = "com.bs.custom.api.domain", basePackageClasses = Jsr310JpaConverters.class)
@EnableJpaRepositories(basePackages = "com.bs.custom.api.repository")
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@EnableAsync
public class CustomServicesConfiguration {
static {
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
}
}
解决方案
我已经修改了我的 WebApplication 类以从 SpringBootServletInitializer 扩展,如Spring Boot 参考指南中所定义(感谢M.Deinum)
最终的 WebApplication 类源代码如下:
@SpringBootApplication
@EnableCustomConfiguration
@EnableCaching
@EnableScheduling
@EnableAsync
public class WebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
}
现在,异步方法可以在 JBoss 上正确运行