springboot学习入门简易版七---springboot2.0使用@Async异步执行方法(17)
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)的更多相关文章
- springboot学习入门简易版三---springboot2.0启动方式
2.4使用@componentscan方式启动 2.4.1 @EnableAutoConfiguration 默认只扫描当前类 @EnableAutoConfiguration 默认只扫描当前类,如果 ...
- springboot学习入门简易版二---springboot2.0项目创建
2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...
- springboot学习入门简易版八---springboot2.0多环境配置、整合mybatis mysql8+(19-20)
2.11 SpringBoot多环境配置(19) application.properties中配置 Spring.profiles.active=prd 配置环境: Application-dev ...
- springboot学习入门简易版六---springboot2.0整合全局捕获异常及log4j日志(12-13)
使用Aop实现 1创建异常请求 在原有项目基础上,jspController中创建一个可能发生异常的请求: /** * 全局捕获异常测试 * @param i * @return */ @Reques ...
- springboot学习入门简易版五---springboot2.0整合jsp(11)
springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包. 1 创建maven项目 注意:必须为war类型,否则找不到页面. 且不要把js ...
- springboot学习入门简易版九---springboot2.0整合多数据源mybatis mysql8+(22)
一个项目中配置多个数据源(链接不同库jdbc),无限大,具体多少根据内存大小 项目中多数据源如何划分:分包名(业务)或注解方式.分包名方式类似多个不同的jar,同业务需求放一个包中. 分包方式配置多数 ...
- springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层
2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static /public /resourc ...
- springboot学习入门简易版一---springboot2.0介绍
1.1为什么用springboot(2) 传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦.Tomcat容器加载web.xml配置内容 springboot完全采用注解化(使用注解方式启动 ...
- SpringBoot学习入门之Hello项目的构建、单元测试和热部署等(配图文,配置信息详解,附案例源码)
前言: 本文章主要是个人在学习SpringBoot框架时做的一些准备,参考老师讲解进行完善对SpringBoot构建简单项目的学习汇集成本篇文章,作为自己对SpringBoot框架的总结与笔记. 你将 ...
随机推荐
- MySql通过数据库文件恢复数据库
以表”Table”为例: 如类型是MyISAM, 数据文件则以”Table.frm””Table.MYD””Table.MYI””三个文件存储于”/data/$databasename/”目录中. 如 ...
- Oracle系列十一 数据处理
数据操纵语言 DML(Data Manipulation Language – 数据操纵语言) 可以在下列条件下执行: 向表中插入数据 修改现存数据 删除现存数据 事务是由完成若干项工作的DML语句组 ...
- springMVC中controller的传参的几种案例
1.springmvc的controller方法不指定method时,默认get/post都支持 //@RequestMapping(value="test") //@Reques ...
- spring boot集成mybatis分页插件
mybatis的分页插件能省事,本章记录的是 spring boot整合mybatis分页插件. 1.引入依赖 <!-- 分页插件pagehelper --> <dependency ...
- Python分词、情感分析工具——SnowNLP
本文内容主要参考GitHub:https://github.com/isnowfy/snownlp what's the SnowNLP SnowNLP是一个python写的类库,可以方便的处理中文文 ...
- matlab学习笔记7-定时器
一起来学matlab-matlab学习笔记7-定时器 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰等著 感谢张老师的书籍,让 ...
- [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- Java之浮点数运算
浮点数运算和整数运算相比,只能进行加减乘除这些数值运算,不能做位运算和移位运算. 在计算机中,浮点数虽然表示的范围很大,但是浮点数有个非常重要的特点,就是浮点数常常无法精确表示 举例 浮点数0.1在计 ...
- 简单的Lock死锁例子
static void Main(string[] args) { lock (_lock1) { var t = new Thread(() => { lock (_lock1) { Cons ...
- 给通过canvas生成的二维码添加logo
以jquery.qrcode为例来说, 生成二维码代码: 依赖jquery.js, jquery.qrcode.js //计算宽,高,中心坐标,logo大小 var width = 256,heigh ...