zoukankan      html  css  js  c++  java
  • spring的@Async异步使用

    pring的@Async功能,用的时候一定要注意:

    1、异步方法和调用类不要在同一个类中。

    2、xml里需要加入这一行 <task:annotation-driven/>

    下面的可以直接粘贴使用

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    以下是demo

    @Autowired
        private AsyncDemo asyncDemo;
    
        @Action("/a")
        public String a() throws Exception {
            asyncDemo.asyncInvokeWithParameter(paramString("t"));
            System.out.println(1111);
            return null;
        }
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    @Service
    public class AsyncDemo {
    
    
        @Async
        public void asyncInvokeWithParameter(String s) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           System.out.println("asyncInvokeWithParameter, parementer={}"+ s);
        }
    
    
    }
  • 相关阅读:
    生成函数初步
    Lg 8月赛(构造+交互)
    wqs 二分学习笔记
    FastAPI 学习之路(十六)Form表单
    线性代数入门
    Oracle-PDB拔插
    MySQL-audit审计插件
    MySQL-用户与权限管理
    MySQL-存储引擎
    MySQL-逻辑结构
  • 原文地址:https://www.cnblogs.com/shihaiming/p/8649666.html
Copyright © 2011-2022 走看看