今日份学习: Spring中使用AOP并实现redis缓存?
笔记
在Spring中如何使用AOP?
- Spring是如何切换JDK动态代理和CGLIB的?
- spring.aop.proxy-target-class=true (在下方第二个链接中,原生doc中提到过)
- @Aspect生命切面
- @Before
- @After
- @Around
Redis
- 广泛使用的内存缓存
- 常见的数据结构:
- String
- List
- Set
- Hash
- ZSet
- Redis为什么快?
- 完全基于内存
- 优秀的数据结构设计
- 单一线程,避免上下文切换开销
- 事件驱动,非阻塞
浏览的一些学习资料
Spring Boot中使用AOP统一处理Web请求日志
Proxying mechanisms: 代理机制(spring doc)
Aspect Oriented Programming with Spring: 使用AOP进行面向切面编程(spring doc)
2020年2月8日 更新:
如何使用再spring boot中redis?
这里我使用了docker容器
- 首先引入pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- docker中运行redis,端口为6379
docker run -p 6379:6379 -d redis
- 创建了一个名为config的package,并创建了config/AppConfig.java
@Configuration
public class AppConfig {
@Bean
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}
使用AOP实现redis的缓存(代码)
@Aspect
@Configuration
public class CacheAspect {
// Map<String, Object> cache = new HashMap<>();
@Autowired
RedisTemplate<String,Object> redisTemplate;
@Around("@annotation(emmm.anno.Cache)")
public Object cache(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getName();
Object cachedValue = redisTemplate.opsForValue().get(methodName);
// Object cachedValue = cache.get(methodName);
if (cachedValue != null) {
System.out.println("from cache");
return cachedValue;
} else {
System.out.println("from db");
Object realValue = joinPoint.proceed();
redisTemplate.opsForValue().set(methodName, realValue);
// cache.put(methodName, realValue);
return realValue;
}
}
}
补:参考到的网址:
docker->redis
docker start
spring doc -> spring data redis
Spring Boot中使用Redis数据库
今日份学习: Spring中使用AOP并实现redis缓存?的更多相关文章
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- Spring 中基于 AOP 的 @AspectJ
Spring 中基于 AOP 的 @AspectJ @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格. 通过在你的基于架构的 XML ...
- Spring 中基于 AOP 的 XML架构
Spring 中基于 AOP 的 XML架构 为了使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述: <?xml version="1.0" e ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- 学习spring中遇见的问题
报错: Unexpected exception parsing XML document from class path resource [applicationContext.xml]; nes ...
- Spring中关于AOP的实践之概念
一.什么是AOP AOP:也称作面向切面编程 在分享几个概念执行我想先举个栗子(可能例子举得并不是特别恰当): 1.假如路人A走在大街上,被一群坏人绑架了: 2.警察叔叔接到报警迅速展开行动:收集情报 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...
随机推荐
- nginx、apache和tomcat之间的关系和区别
Apache/Nginx 应该叫做 HTTP Server,即安装后生成httpd服务. Tomcat 则是一个 Application Server,或者更准确的来说,是一个「Servlet/JSP ...
- 「JSOI2014」电信网络
「JSOI2014」电信网络 传送门 一个点选了就必须选若干个点,最大化点权之和,显然最大权闭合子图问题. 一个点向它范围内所有点连边,直接跑最大权闭合子图即可. 参考代码: #include < ...
- 「SDOI2005」区间
「SDOI2005」区间 传送门 记录每一个位置作为左端点和右端点的出现次数,然后直接考虑差分即可. 参考代码: #include <cstdio> #define rg register ...
- 操作系统OS - 反置页表
1. https://blog.csdn.net/wuyuegb2312/article/details/16359821 2. https://www.youtube.com/watch?v=YQ3 ...
- build hadoop, spark, hbase cluster
1,something: 1,arc land 506 git branch 507 git status 508 git reset multicloud/qcloud/cluster_man ...
- Mac. 修改bash_file
https://www.cnblogs.com/mokey/p/3542389.html
- 【原】nginx配置文件
一:下载nginx方式 1.yum install nginx 2.源码安装 二:学习网址 nginx documentation — DevDocs 三:配置文件信息 server { listen ...
- 查看mysql进程
show processlist; show full processlist;
- Python学习笔记009
不换行 print("Hello,world!",end='')print("Hello,world!",end='')print("Hello,wo ...
- Linux内核5.4正式将华为EROFS超级文件系统合入主线
导读 近期,Linux内核5.4系列宣布全面可用,添加了许多新功能,更强的安全性和更新的驱动程序,以提供更好的硬件支持.Linux内核5.4增加对微软exFAT文件系统的支持,另外还支持内核锁定功能, ...