springboot 中怎么利用@Async實(shí)現(xiàn)異步調(diào)用,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)公司專注于密山企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,購(gòu)物商城網(wǎng)站建設(shè)。密山網(wǎng)站建設(shè)公司,為密山等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
添加注解@EnableAsync開啟異步調(diào)用
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableAsync
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}在被調(diào)用方法上加 @Async
package com.demo.serviceImp;
import com.demo.serviceI.DemoService;
import org.springframework.scheduling.annotation.Async;
public class DemoServiceWindowImp implements DemoService {
@Override
public String info() {
return "window";
}
@Override
@Async
public void print(int i) {
System.out.println(i);
}
}調(diào)用測(cè)試
package com.demo.action;
import com.demo.serviceI.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoAction {
@Autowired
private DemoService demoService;
@RequestMapping(value = "health.json")
public String healt(){
return "{\"status\":\"UP\",\"diskSpace\":{\"status\":\"UP\",\"total\":249769230336,\"free\":71914618880,\"threshold\":10485760},\"db\":{\"status\":\"UP\",\"database\":\"MySQL\",\"hello\":1}}";
}
@RequestMapping(value = "user/info")
public String info(){
return demoService.info();
}
@RequestMapping(value = "print")
public void print(){
for (int i = 0; i < 100; i++) {
demoService.print(i);
}
}
}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
本文名稱:springboot中怎么利用@Async實(shí)現(xiàn)異步調(diào)用
分享URL:http://chinadenli.net/article40/jhgeho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、手機(jī)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、網(wǎng)站維護(hù)、軟件開發(fā)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)