1.Aware系列接口

spring 6.0提供了一系列的Aware接口,方便我们在Bean加载时获取信息

@Service
public class study implements BeanNameAware {
// spring实例化bean之后调用此方法
@Override
public void setBeanName(String name) {
System.out.println(name);
}
}
结果
study

2.Async异步任务

  1. Spring入口必须开启异步,添加@EnableAsync注解
  2. 必须被spring托管的bean
  3. 在方法上添加@Async注解
@EnableAsync
@SpringBootApplication
public class SpringSecurityTestApplication { public static void main(String[] args) {
ApplicationContext run = SpringApplication.run(SpringSecurityTestApplication.class, args);
Study bean = run.getBean(Study.class);
System.out.println("main方法开始...");
bean.asyncText();
System.out.println("main方法结束...");
} }
@Component()
public class Study { @Async
public void asyncText() {
System.out.println("异步执行开始");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("异步执行结束");
}
}
结果
main方法开始...
main方法结束...
异步执行开始
异步执行结束

3.定时任务

  1. 在Spring入口添加@EnableScheduling注解
  2. 必须被Spring托管
  3. 在方法添加@Scheduled注解
@EnableScheduling
@SpringBootApplication
public class SpringSecurityTestApplication { public static void main(String[] args) {
ApplicationContext run = SpringApplication.run(SpringSecurityTestApplication.class, args);
Study bean = run.getBean(Study.class);
System.out.println("main方法开始...");
bean.task();
System.out.println("main方法结束...");
} }
@Component()
public class Study { // fixdRate 固定毫秒执行,不管上次是否执行完毕
// fixedDelay 继上次执行后固定毫秒执行
// cron 自定义规则
@Scheduled(fixedRate = 2000)
public void task() {
System.out.println("我是定时任务"+ new Date());
}
}
结果
main方法开始...
我是定时任务Thu Feb 23 08:16:24 CST 2023
main方法结束...
我是定时任务Thu Feb 23 08:16:26 CST 2023
我是定时任务Thu Feb 23 08:16:28 CST 2023
我是定时任务Thu Feb 23 08:16:30 CST 2023
我是定时任务Thu Feb 23 08:16:32 CST 2023
我是定时任务Thu Feb 23 08:16:34 CST 2023
...

最新文章

  1. python2-gst0.10制作静态包的补丁 v1.1
  2. 两个平行div之间有间隙
  3. CSS选择器性能分析
  4. apache 2.4 配置多个站点
  5. Python学习教程(learning Python)--3.3.3 Python逻辑关系表达式
  6. 【python】 开始第一个项目
  7. Raspberry Pi + 3个USB摄像头 + Motion(简易监控设备配置记录1——介绍以及安装) 分类: Raspberry Pi 服务器搭建 2015-04-12 19:21 226人阅读 评论(0) 收藏
  8. maven jetty运行命令
  9. AngularJS 模板
  10. 【SSH系列】静态代理&&动态代理
  11. Windows下的Nessus安装与启动
  12. UICollectionView didSelectItemAtIndexPath实现方法
  13. SwingBench 字符模式压测最佳实践
  14. django面试题必问
  15. MySQL数据类型及使用场景
  16. Linux-mkdosfs格式化磁盘命令(15)
  17. Java ScriptEngine 解析js
  18. 内核镜像zImage是如何生成的
  19. git学习笔记6
  20. Invalid default value for prop "value": Props with type Object/Array must use a factory function to return the default value.(props default 数组/对象的默认值应当由一个工厂函数返回)

热门文章

  1. MongoDB从入门到实战之MongoDB快速入门
  2. CompletableFuture 使用总结
  3. 2020强网杯青少赛Pursuing_The_Wind战队WRITEUP
  4. [python] 基于matplotlib实现雷达图的绘制
  5. UOJ33 [UR#2] 树上 GCD
  6. 1_ios系统httpstatus状态为0
  7. 火山引擎DataLeap数据调度实例的 DAG 优化方案
  8. 聊聊web漏洞挖掘第一期
  9. DVWA靶场实战(七)——SQL Injection
  10. Loj 507 接竹竿 题解