今日份学习: 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:面向方面编程.不 ...
随机推荐
- 洛谷 P3009 [USACO11JAN]利润Profits
嗯... 题目链接:https://www.luogu.org/problemnew/show/P3009 这是DP的另一个功能,求最大子段和(最大子段和模板:https://www.luogu.or ...
- java之中文乱码处理
有些时候,比如文件操作的时候,特别是文件中有中文,会规定用GBK格式,这时读写文件,可能会出现中文乱码 资源文件乱码 文件内容乱码 资源文件乱码: 解决: PropertiesUtil proper ...
- lnmp1.5安装fileinfo扩展
cd /usr/local/src cd lnmp1.-full/src tar xvf php-.tar.bz2 cd php-/ext/fileinfo phpize ./configure -- ...
- 设计模式课程 设计模式精讲 6-2 抽象工厂coding
1 代码讲解 1.1 抽象工厂优点 1.2 抽象工厂缺点 1.3 为何有产品族的业务场景宜用抽象工厂设计模式?而不是工厂设计模式? 2 代码演练 2.1 抽象工厂代码演练 1 代码讲解 1.1 抽象工 ...
- selenium webdriver 执行Javascript
@Test public void testElementByID() { //通过JS获取页面元素 driver.get(url); driver.manage().window().maximiz ...
- windows下创建/删除服务
windows下创建/删除服务 1. windows下创建/删除服务 1.1. 创建服务 命令格式: sc [servername] create Servicename [Optio ...
- 汇编语言从入门到精通-2CPU资源和存储器
CPU资源和存储器 在汇编语言中,需要访问的硬件资源主要有:CPU内部资源.存储器和I/O端口.本章将着重讲解CPU内部寄存器的命名.功能及其常见的用途,还要介绍存储器的分段管理模式.存储单元地址的表 ...
- Java记录2---包的使用
javac -d . A.java -d 表示自动生成包层 . 表示这个包层在当前目录下建立 package link.roland;//package 语句必须是第一条语句 //该语句表示把该文件中 ...
- 如何用C++读取图片中的像素
来源:https://bbs.csdn.net/topics/391956973 3楼 #include <iostream> #include <fstream> #inc ...
- IP地址规划
IP地址(Internet Protocol Address),缩写为IP Adress,是一种在Internet上的给主机统一编址的地址格式,也称为网络协议(IP协议)地址.它为互联网上的每一个网络 ...