1启动类开启异步调用注解

@SpringBootApplication
@EnableAsync //开启异步调用
public class StartApplication {

不开启则异步调用无效

2编写异步调用方法

@RestController
public class AsyncController {
private final static Logger logger=LoggerFactory.getLogger(WebLogAspect.class);
@Autowired
private AsyncService asyncService; @RequestMapping("/testAsync")
public String testAsync() {
logger.info("1");
String result=asyncService.asynTest();
logger.info("4");
return result;
}
}
@Service
public class AsyncService {
private final static Logger logger=LoggerFactory.getLogger(WebLogAspect.class); @Async //相当于重新开辟单独线程执行该方法
public String asynTest() {
logger.info("2");
try {
Thread.sleep(5000);
}catch(Exception e) { }
logger.info("3");
return "success";
}
}

3 访问:http://localhost:8080/testAsync

页面显示空,后台日志:

说明异步调用成功,未按顺序执行。

原理:使用aop技术在运行时创建一个单独线程执行

github代码:https://github.com/cslj2013/springboot2.0_log_aop.git

springboot学习入门简易版七---springboot2.0使用@Async异步执行方法(17)的更多相关文章

  1. springboot学习入门简易版三---springboot2.0启动方式

    2.4使用@componentscan方式启动 2.4.1 @EnableAutoConfiguration 默认只扫描当前类 @EnableAutoConfiguration 默认只扫描当前类,如果 ...

  2. springboot学习入门简易版二---springboot2.0项目创建

    2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...

  3. springboot学习入门简易版八---springboot2.0多环境配置、整合mybatis mysql8+(19-20)

    2.11 SpringBoot多环境配置(19)  application.properties中配置 Spring.profiles.active=prd 配置环境: Application-dev ...

  4. springboot学习入门简易版六---springboot2.0整合全局捕获异常及log4j日志(12-13)

    使用Aop实现 1创建异常请求 在原有项目基础上,jspController中创建一个可能发生异常的请求: /** * 全局捕获异常测试 * @param i * @return */ @Reques ...

  5. springboot学习入门简易版五---springboot2.0整合jsp(11)

    springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包. 1 创建maven项目 注意:必须为war类型,否则找不到页面. 且不要把js ...

  6. springboot学习入门简易版九---springboot2.0整合多数据源mybatis mysql8+(22)

    一个项目中配置多个数据源(链接不同库jdbc),无限大,具体多少根据内存大小 项目中多数据源如何划分:分包名(业务)或注解方式.分包名方式类似多个不同的jar,同业务需求放一个包中. 分包方式配置多数 ...

  7. springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

    2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resourc ...

  8. springboot学习入门简易版一---springboot2.0介绍

    1.1为什么用springboot(2) 传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦.Tomcat容器加载web.xml配置内容 springboot完全采用注解化(使用注解方式启动 ...

  9. SpringBoot学习入门之Hello项目的构建、单元测试和热部署等(配图文,配置信息详解,附案例源码)

    前言: 本文章主要是个人在学习SpringBoot框架时做的一些准备,参考老师讲解进行完善对SpringBoot构建简单项目的学习汇集成本篇文章,作为自己对SpringBoot框架的总结与笔记. 你将 ...

随机推荐

  1. 堆排序Heapsort的Java和C代码

    Heapsort排序思路 将整个数组看作一个二叉树heap, 下标0为堆顶层, 下标1, 2为次顶层, 然后每层就是"3,4,5,6", "7, 8, 9, 10, 11 ...

  2. 安装比特币区块链钱包API(Blockchain Wallet用户发送和接收比特币的简单API)

    区块链钱包API提供了一个简单的界面,商家可以用它以编程方式与钱包进行交互. 安装:要使用此API,您需要运行负责管理区块链钱包的小型本地服务. 您的应用程序通过HTTP API调用在本地与此服务进行 ...

  3. Extjs与Vue技术优劣势比较

    此技术只变更PLM的表单化的页面显示风格,不涉及不影响整个现有平台的架构和蓝图

  4. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  5. 常用gem

    rails_db better_errors awesome_print web-console irbtools binding_of_caller awesome_rails_console

  6. app内嵌h5页面在ios手机端滑动卡顿的解决方法

    1.带滚动条的dom需加样式 -webkit-overflow-scrolling: touch;2.去掉 width:100%; height:100%

  7. PHP 批量删除的实现

    布局效果 布局代码 <button type="button" class="btn btn-sm btn-danger btn-erbi-danger" ...

  8. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...

  9. 【转帖】MIPS构架之:我和龙芯有个约会

    MIPS构架之:我和龙芯有个约会 https://www.eefocus.com/mcu-dsp/364490 <处理器史话>之十二 2016-06-24 12:21 作者:付丽华预计 1 ...

  10. [CMD] 批处理

    https://www.tutorialspoint.com/batch_script/batch_script_commands.htm