java - 如何让 spring-boot @Async 与 Java 8 一起使用
问题描述
我正在尝试异步调用方法。但不知何故,它不起作用。有人可以帮我解决这个问题吗?
我的主要入口点:
@SpringBootApplication
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
AsyncService asyncService = new AsyncService();
asyncService.asyncMethod();
asyncService.asyncMethod();
}
}
异步服务:
@Component
public class AsyncService {
@Async
public void asyncMethod(){
log.info("starting...");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("ending...");
}
}
最后,在日志中我期待:
- 开始...
- 开始...
- 结尾...
- 结尾...
但这就是我得到的:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE)
2019-02-13 17:52:41.548 INFO 85734 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on mntbden00122972 with PID 85734 (/Users/h3560/demo/target/classes started by h3560 in /Users/h3560/demo)
2019-02-13 17:52:41.550 INFO 85734 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-02-13 17:52:42.084 INFO 85734 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 0.76 seconds (JVM running for 1.329)
2019-02-13 17:52:42.086 INFO 85734 --- [ main] com.example.demo.services.AsyncService : starting...
2019-02-13 17:52:44.088 INFO 85734 --- [ main] com.example.demo.services.AsyncService : ending...
2019-02-13 17:52:44.089 INFO 85734 --- [ main] com.example.demo.services.AsyncService : starting...
2019-02-13 17:52:46.091 INFO 85734 --- [ main] com.example.demo.services.AsyncService : ending...
解决方案
@Async
是 spring 的注释,它被应用于public
代理上的方法(这就是它们需要公开的原因)。自调用不起作用。
在您的示例中,您没有使用 spring 的依赖注入机制,因此没有创建代理。要绕过它,您需要在执行之前对其进行处理@Bean
(您已经通过使用 注释来完成@Component
)和@Autowire
它:
@SpringBootApplication
@EnableAsync
public class DemoApplication {
@Autowired
AsyncService asyncService;
public someMethod() {
SpringApplication.run(DemoApplication.class, args);
asyncService.asyncMethod();
asyncService.asyncMethod();
}
}
有了这个 spring AOP 可以将组件包装到代理中并异步执行方法。
推荐阅读
- c++ - Fmod 函数清楚地输出预期的双精度,但 if(fmod == 预期的双精度) 不会评估为真
- javascript - 如何为单独的 webpack 服务器/客户端配置正确覆盖 babel@7 插件
- mysql - 返回主题标签帖子的数量 sql, mysql
- c - 在 C 中,如何获取捕获组 RegEx?
- influxdb - 找不到时区
- php - 为什么当 CSRF 为 TRUE 时我无法在 codeigniter 中的 Dropzone 上上传文件
- scala - 如何在 scalaz 中对 Liskov 的 trans 函数进行类型缩减?
- numpy - 为什么numpy数组的大小不同?
- python - 在python中使用列表查找素数但出错
- algorithm - 最小化 3-d 中点集的欧几里得距离