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. Linux中mpstat命令参数详解

    Linux中mpstat命令参数详解 mpstat 是 Multiprocessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在 /proc/stat ...

  2. 用于KV集群的一致性哈希Consistent Hashing机制

    KV集群的请求分发 假定N为后台服务节点数,当前台携带关键字key发起请求时,我们通常将key进行hash后采用模运算 hash(key)%N 来将请求分发到不同的节点上, 后台节点的增删会引起几乎所 ...

  3. PostgreSQL中的pg_relation_filepath()函数

    pg_relation_filepath()类似于pg_relation_filenode(),但它返回关系的整个文件路径名(相对于数据库集群的数据目录PGDATA). postgres=# sele ...

  4. [译]使用to_dict将pandas.DataFrame转换为Python中的字典列表

    pandas.DataFrame.to_json返回的是JSON字符串,不是字典. 可以使用to_dict进行字典转换. 使用orient指定方向. >>> df col1 col2 ...

  5. 【LeetCode算法-27】Remove Element

    LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...

  6. windows下Java调用可执行文件

    缘起: 由于没有找到java转换文件的接口,因此使用java调用exe文件进行文件转换 public void convertFile(){ Runtime rn = Runtime.getRunti ...

  7. Saltstack限制某些shell命令执行

    在cmdmod模块中cmd.run.cmd.run_all.cmd.run_stdout等都可以执行shell命令,要静止某些shell命令,可以修改_run()这个函数来彻底的静止调用这个命令. c ...

  8. centos7 intall nvidia driver

    此教程是介绍于 CentOS 7 以上的 Linux 系统中安装 NVIDIA 显卡驱动和 CUDA Toolkit .此文中以 CentOS 7.4 64 bit 为例,显卡型号为 NVIDIA T ...

  9. docker容器中用户自定bridge网络与默认bridge网络之间的区别

    转载 https://blog.csdn.net/dkfajsldfsdfsd/article/details/79959534

  10. React路由传参的三种方式

    方式 一:          通过params         1.路由表中                     <Route path=' /sort/:id '   component= ...