SpringBoot异步方法如何使用及注意事项

首先新建一个启动类

启动类上必须添加注解@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("===================================================");
    }

}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页