spring 特性
2024-09-08 19:04:40
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异步任务
- Spring入口必须开启异步,添加@EnableAsync注解
- 必须被spring托管的bean
- 在方法上添加@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.定时任务
- 在Spring入口添加@EnableScheduling注解
- 必须被Spring托管
- 在方法添加@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
...
最新文章
- python2-gst0.10制作静态包的补丁 v1.1
- 两个平行div之间有间隙
- CSS选择器性能分析
- apache 2.4 配置多个站点
- Python学习教程(learning Python)--3.3.3 Python逻辑关系表达式
- 【python】 开始第一个项目
- Raspberry Pi + 3个USB摄像头 + Motion(简易监控设备配置记录1——介绍以及安装) 分类: Raspberry Pi 服务器搭建 2015-04-12 19:21 226人阅读 评论(0) 收藏
- maven jetty运行命令
- AngularJS 模板
- 【SSH系列】静态代理&;&;动态代理
- Windows下的Nessus安装与启动
- UICollectionView didSelectItemAtIndexPath实现方法
- SwingBench 字符模式压测最佳实践
- django面试题必问
- MySQL数据类型及使用场景
- Linux-mkdosfs格式化磁盘命令(15)
- Java ScriptEngine 解析js
- 内核镜像zImage是如何生成的
- git学习笔记6
- Invalid default value for prop ";value";: Props with type Object/Array must use a factory function to return the default value.(props default 数组/对象的默认值应当由一个工厂函数返回)
热门文章
- MongoDB从入门到实战之MongoDB快速入门
- CompletableFuture 使用总结
- 2020强网杯青少赛Pursuing_The_Wind战队WRITEUP
- [python] 基于matplotlib实现雷达图的绘制
- UOJ33 [UR#2] 树上 GCD
- 1_ios系统httpstatus状态为0
- 火山引擎DataLeap数据调度实例的 DAG 优化方案
- 聊聊web漏洞挖掘第一期
- DVWA靶场实战(七)——SQL Injection
- Loj 507 接竹竿 题解