欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

相关标签: Java