今日份学习: 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:面向方面编程.不 ...
随机推荐
- %E3%80%90%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E3%80%91
"%3Cdiv%20class%3D%22htmledit_views%22%20id%3D%22content_views%22%3E%0A%20%20%20%20%20%20%20%20 ...
- 题解【[Ynoi2012]NOIP2015洋溢着希望】
\[ \texttt{Preface} \] 第二道 Ynoi 的题,纪念一下. 这可能是我唯一可以自己做的 Ynoi 题了. \[ \texttt{Description} \] 维护一个长度为 \ ...
- 解决github访问慢和下载项目慢的问题
一.国内访问 GitHub 为什么很慢? GitHub的CDN域名遭到DNS污染,导致无法连接使用 GitHub 的加速分发服务器,才使得国内访问速度很慢. 二.如何解决 DNS 污染? 通过修改 ...
- Layui我提交表单时,table.reload(),表格会请求2次,是为什么?
重载两次是因为搜索按钮用的是button 改成<a class="layui-btn" data-type="reload">搜索</a> ...
- linux下后台执行shell脚本nohup
(一)使用nohup后台执行脚本 脚本执行结果记录到nohup.out文件中 (二)使用&后台执行脚本 使用&符号在后台执行命令或脚本后,如果你退出登录,这个命令就会被自动终止掉
- nginx 的请求处理阶段
nginx处理的11个阶段 nginx处理用户请求的流程 接收用户请求头部之后 1 .匹配对应得location 2.是否进行限速 3.验证用户是否有权限访问该资源:和判断是否是盗链的请求 4.生成用 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:在 <tbody> 内的任一行启用鼠标悬停状态
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- jdk 档案库(包含历史版本)
http://java.sun.com/products/archive/ 参考:https://blog.csdn.net/shiluyong8068/article/details/7894747 ...
- 《SQL 进阶教程》 查找局部不一致的数据
-- 从下面这张商品表里找出价格相等的商品的组合 select * from products p1LEFT JOIN products p2on p1.price = p2.price and p1 ...
- IP地址规划
IP地址(Internet Protocol Address),缩写为IP Adress,是一种在Internet上的给主机统一编址的地址格式,也称为网络协议(IP协议)地址.它为互联网上的每一个网络 ...