码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot实现异步

时间:2018-12-02 10:33:33      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:org   .com   ack   style   nbsp   async   imp   rgs   get   

1、创建AsyncTest类

package com.cppdy.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class AsyncTest {
    
    @Async
    public void asyncOut() {
        System.out.println("异步方法id:"+Thread.currentThread().getId());
    }

}

2、在UserController中创建测试方法

@RequestMapping("async")
    public String async() {
        System.out.println("Main Thread Id:"+Thread.currentThread().getId());
        asyncTest.asyncOut();
        return "async";
    }

3、在Application类中开启异步(@EnableAsync)

package com.cppdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 

SpringBoot实现异步

标签:org   .com   ack   style   nbsp   async   imp   rgs   get   

原文地址:https://www.cnblogs.com/cppdy/p/10051968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!