-
SpingBoot可以是秒级别的创建项目, 创建项目的速度很快 而且无需xml文件配置, 可以直接运行文件, 其内部提供了内置的容器, 可以直接运行项目和发布项目
-
还有一点就是今天学到的东西提供了监控的指标, 方便监控项目的运行情况, 提供了起步的依赖, 急速添加框架的支持
缓存的优点
-
使⽤缓存可以避免访问数据库,节省数据库服务器的资源
-
性能更快,缓存属于内存级的服务器,⽽ DB 牵扯到更多的业务逻辑判断和磁盘操作,因此缓存的性能更⾼。
-
数据库一般不能做集群,而缓存redis做集群非常简单
开启缓存
package listen.example3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching //开启缓存的注解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
敲缓存编辑代码
实现缓存数据的增删改查
package listen.example3.serviceDemo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will a loser.
-
User: Listen-Y.
-
Date: 2020-08-23
-
Time: 15:36
*/
@Service
public class PersonService {
//这里我们首先自定义一个类和对想, 将这个对象保存在缓存中
static class Person{
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
private static Person person = new Person(“listen”, 21);
//缓存的查询 并将查到的数据保存至缓存中
@Cacheable(cacheNames = “person”, key = “#id”)
public Object getPersonById(int id) throws InterruptedException {
//我通过id查询我的person 如果此时缓存中没有这个id对应的person就会进行查询
System.out.println(“进入查询…”);
//方便我们演示如果进入查询我们多等待3秒
TimeUnit.SECONDS.sleep(3);
if (id == 1) {
return person;
}
return “Id错误…”;
}
//修改这个id绑定的对象的属性
@CachePut(cacheNames = “person”, key = “#id”)
public Object updatePerson(int id, String newName) {
System.out.println(“进入修改方法…”);
if (id == 1) {
person.name = newName;
return person;
}
return “Id错误…”;
}
//删除缓存
@CacheEvict(cacheNames = “person”, key = “#id”)
public boolean delPerson(int id) {
return id == 1;
}
}
编写触发代码
package listen.example3.serviceDemo.personController;
import listen.example3.serviceDemo.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will a loser.
-
User: Listen-Y.
-
Date: 2020-08-23
-
Time: 15:54
*/
@RestController
@RequestMapping(value = “per”)
public class PersonController {
//注入对象
@Autowired
private PersonService personService;
//演示获取缓存
@RequestMapping(value = “get”)
public Object getPerson(int id) throws InterruptedException {
return personService.getPersonById(id);
}
//演示修改
@RequestMapping(value = “update”)
public Object updatePerson(int id, String newName) {
return personService.updatePerson(id, newName);
}
//演示删除
@RequestMapping(value = “del”)
public Object delPerson(int id) {
return personService.delPerson(id);
}
}
- 最后运行项目使用postman访问就会发现有缓存的好处
简述原理
- Cache本质上是用conCurrentMap的数据结构实现, 其key便是缓存的cacheName value便是我们的#id中的id, 可以有效节约数据库资源, 加速程序的查询效率
定时任务:顾名思义,定时执⾏的任务我们称之为定时任务。它包含了两层含义,第⼀是定时,第⼆是 任务。也就是说预计某个时间去执⾏某件事的这个⾏为就被称之为定时任务。
为什么需要定时任务?
定时任务的作⽤:防⽌错过某项重要的任务。
开启定时任务功能
package listen.example3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableCaching //开启缓存的注解
@EnableScheduling //开启定时任务的注解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
编写定时任务的代码
package listen.example3.serviceDemo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will a loser.
-
User: Listen-Y.
-
Date: 2020-08-23
-
Time: 16:21
*/
@Component //表示这是一个组件
public class SchedulingService {
//下面的cron表达式表示不管何时何月何年从启动开始 每三秒执行一次
//每位代表含义:秒 分钟 ⼩时 ⽇ ⽉ 周 年
@Scheduled(cron = “0/3 * * * * ?”)
public void schedule() {
System.out.println(“定时任务执行中…”);
}
}
cron表达式
cron 表达式可以是 6 位或者是 7 位,每个位置从左到右代表的含义如下:
最后运行代码就会发现定时任务的存在
简述定时任务原理
SpringBoot 在初始化 bean 之后,通过拦截器(postProcessAfterInitialization)拦截到所有⽤到 @Scheduled 注解的⽅法,再将这些⽅法放⼊到定时任务列表中,然后按照顺序执⾏这些定时任务。 定时任务的执⾏细节是,先解析 cron 表达式,获取到定时任务下次执⾏的时间,然后放⼊线程中等到对 应的时间到了之后进⾏执⾏,之后会按照执⾏的频次,定时执⾏这些任务。
常用cron表达式
(1)0/2 * * * * ? 表示每2秒 执行任务
(1)0 0/2 * * * ? 表示每2分钟 执行任务
(1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED 表示每个星期三中午12点
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
无助。**
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-wmB9neRd-1715071437154)]
[外链图片转存中…(img-6A85of3X-1715071437155)]
[外链图片转存中…(img-xsDlxDRH-1715071437155)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!