Spring cache 配置代理 改为aspectj
来源:互联网 发布:java协程 编辑:程序博客网 时间:2024/05/17 22:58
1 Spring cache 配置代理 改为aspectj
1.1 使用配置
1) 添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
</dependency>
2) Spring xml配置
<context:load-time-weaver />
<context:component-scan base-package="com.xxx.examples"/>
<cache:annotation-driven cache-manager="cacheManager" mode="aspectj"/>
<!-- spring cache with jdk map -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default"/>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="product"/>
</set>
</property>
</bean>
3) 添加aop.xml
apo.xml 名称固定,位置放在CLASSPATH 下的META-INF/aop.xml
<aspectj>
<weaver options="-verbose -showWeaveInfo">
<include within="com.xxx.examples.cache..*"/>
</weaver>
</aspectj>
4) 编写缓存使用代码
如:
5) 启动方式
因为使用的aspect 加载时候 改变字节码,所以启动方式Vm上要加个VM 参数
-javaagent:C:\Users\xxx\.m2\repository\org\springframework\spring-instrument\4.3.5.RELEASE\spring-instrument-4.3.5.RELEASE.jar
下图是我的main 方法:
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("context-business-cache.xml"); ProductService productService = ctx.getBean(ProductService.class); productService.queryByIdOut("1"); productService.queryByIdOut("1");}
下图是我运行的结果:
查询DB16:47:20.858 [main] DEBUG org.terracotta.offheapstore.paging.UpfrontAllocatingPageSource - Allocating a 1MB buffer from chunk 0 &52323942416:47:20.859 [main] DEBUG org.terracotta.offheapstore.paging.OffHeapStorageArea - Data area expanded from 1MB to 2MB [occupation=0.0]
只查询了一次db 说明我的配置已经生效
1.2 参考资料
Spring cache配置:
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/cache.html
Spring aspectj 配置:
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-aj-ltw-spring
- Spring cache 配置代理 改为aspectj
- Aspectj的接入使用用于配置Spring自动的AOP代理
- 了解Spring Aspectj创建动态代理
- spring代理注解<aop:aspectj-autoproxy />
- Spring Aspectj 代理 前置 后置 以及 异常
- Spring Aspectj 代理 前置 后置 以及 异常
- Spring Aspectj代理 前置 后置以及异常
- Spring Aspectj 代理 前置 后置 以及 异常
- Spring Aspectj代理 前置 后置以及异常
- Spring Aspectj 代理 前置 后置 以及 异常
- Spring Aspectj 代理 前置 后置 以及 异常
- 基于@AspectJ配置Spring AOP
- Spring Aspectj AOP配置学习
- Spring之配置使用@AspectJ
- Spring AOP Aspectj 注解配置
- Spring配置---<aop:aspectj-autoproxy />
- 配置@aspectj-autoproxy切面,生成代理对象
- Spring boot配置cache
- redis基础简介(五)- 数据备份与恢复、数据安全、性能测试、客户端连接、分区
- 构造函数线程安全-即dubbo的extensionLoader存在线程安全问题
- This version of Android Studio is incompatible with the Gradle Plugin used.
- Hive动态分区
- C语言之变量类型和存储方式
- Spring cache 配置代理 改为aspectj
- 新年新气象,新的一年新的开始,给自己定个小小的目标,以此为证
- spring-boot配置文件-security(中文翻译)
- JS中的this指向相关
- 自定义控件实践——单选/多选文本标签流
- centos下安装nginx
- hibernate联合主键
- C++所有容器的共同操作
- 告别2016,展望2017