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);
        }
    
    
    }
  • 相关阅读:
    Java语法基础-final关键字
    Java语法基础-异常处理
    Java语法基础-序列化
    Java的知识储备及面试-几个方面
    一篇分析测试开发人员的职业发展方向的好文章-学习笔记
    web 自动化测试 selenium基础到应用(目录)
    打包Scala jar 包的正确步骤
    IT项目管理
    寄存器 & 汇编指令
    Window环境下编写Shellcode(入门篇)
  • 原文地址:https://www.cnblogs.com/shihaiming/p/8649666.html
Copyright © 2011-2022 走看看