SpringBoot异步方法如何使用及注意事项
程序员文章站
2024-01-24 14:51:58
首先新建一个启动类启动类上必须添加注解@EnableAsync,表示开启异步方法的使用package com.yuce.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableAsync;@Spr...
首先新建一个启动类
启动类上必须添加注解@EnableAsync,表示开启异步方法的使用
package com.yuce.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
写一个供于测试的异步调用类
要想异步方法有效,一定要将这个类注入ioc容器,可以添加@Component,@Service,@Controller,@Repository等注解
package com.yuce.demo;
import com.yuce.demo.config.PrintX;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class TestAsync{
@Autowired
PrintX printX;
public void testMain() {
for (int i=0;i<100;i++) {
log.info("运行到第{}个,线程{}",i,Thread.currentThread().getName());
printX.print(i);
}
}
@Async
public void testOne() throws InterruptedException {
log.info("testOne.......");
Thread.sleep(10000);
log.info("testOne is end......");
}
@Async
public void testTwo() throws InterruptedException {
log.info("testTwo.......");
Thread.sleep(10000);
log.info("testTwo is end......");
}
@Async
public void testThree() throws InterruptedException {
log.info("testThree.....");
Thread.sleep(10000);
log.info("testThree is end........");
}
@Async
public void testHundred(int x) throws InterruptedException {
log.info("test{}.....",x);
Thread.sleep(10000);
log.info("test{} is end........",x);
}
}
package com.yuce.demo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class PrintX {
@Async
public void print(int x) {
log.info("x:{},线程{}",x,Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
最后需要添加一个测试类,测试类也有讲究,因为测试用到了注解,所以一定要开启SpringBoot测试
@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
其中DemoApplication.class是启动类,SpringBootTest必须加上classes,否则,无法开启异步方法
package com.yuce.demo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
@Slf4j
public class test {
@Autowired
TestAsync testAsync;
@Test
public void test() {
log.info("===================================================");
testAsync.testMain();
try {
// testAsync.testOne();
// testAsync.testTwo();
// testAsync.testThree();
// for (int i=1;i<=100;i++) {
// testAsync.testHundred(i);
// }
Thread.sleep(100000);
} catch (Exception e) {
e.printStackTrace();
}
log.info("===================================================");
}
}
本文地址:https://blog.csdn.net/zt2650693774/article/details/109613770
下一篇: Python可跨平台实现获取按键的方法
推荐阅读
-
SpringBoot异步方法如何使用及注意事项
-
ios14.7正式版如何更新 ios14.7正式版升级方法及注意事项
-
使用php方法curl抓取AJAX异步内容思路分析及代码分享_php实例
-
使用php方法curl抓取AJAX异步内容思路分析及代码分享,curlajax
-
SpringBoot整合Ureport2报表及常见使用方法
-
jQuery Validate 插件 验证基本使用方法及注意事项详解(代码实例)
-
SpringBoot拦截器原理解析及使用方法
-
Springboot apollo原理及使用方法详解
-
Android 如何使用log4j及注意事项
-
java volatile关键字使用方法及注意事项