首页 > 解决方案 > 如何让 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...");
    }
}

最后,在日志中我期待:

  1. 开始...
  2. 开始...
  3. 结尾...
  4. 结尾...

但这就是我得到的:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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...

标签: javaspringmultithreadingspring-bootjava-8

解决方案


@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 可以将组件包装到代理中并异步执行方法。


推荐阅读