spring cache 配置使用aspectj LTW

spring cache 提供了基于注解的缓存配置方法,其实现原理和事务管理的实现是一样的, 都是通过 spring aop来实现的。spring aop 有一个问题, 默认 aop的实现是使用java 动态代理技术来实现的, 这样就会导致,同一个对象内的方法之间的调用,是不会被aop拦截到的。

要解决这个问题,我们可以选择调整代码的位置外,让缓存的方法和调用它的方法分离在不同的类中,但是这种解决方案是不完美的,会导致原本内聚的类,分散在了不同的地方。

除了调整代码外,还有什么办法能支持这种情况?
使用AspectJ 进行 织入。

AspectJ 织入器weaver 支持三种织入方式:

  • compile-time weaving 使用aspectj 编译器进行编译源码
  • post-compile weaving 对class 文件进行织入
  • load-time weaving(LTW) 当class loader 加载类的时候,进行织入

使用

通过JVM的-javaagent 加载代理,在代理内持有Instrumentation 对象,方便后续的注册class translate hook。

-javaagent:D:\lib\spring-instrument\spring-instrument-4.3.0.RELEASE.jar

spring cache 配置 mode="aspectj"


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:load-time-weaver/>

    <cache:annotation-driven mode="aspectj"/>

    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg ref="redisTemplate"/>
    </bean>

    <bean id="propertyConfigurer3" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="3" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.password}" />
        <property name="usePool" value="true" />
        <property name="poolConfig" ref="poolConfig" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory"   ref="connectionFactory" />
    </bean>


    <bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

</beans>


META-INF/aop.xml 声明需要进行织入的目标类



<aspectj>
    <weaver options="-verbose -showWeaveInfo">
        <include within="com.xxx..*"/>
    </weaver>
</aspectj>


参考 Load-Time Weaving

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 本章内容: 面向切面编程的基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 ...
    谢随安阅读 3,203评论 0 9
  • AOP实现可分为两类(按AOP框架修改源代码的时机): 静态AOP实现:AOP框架在编译阶段对程序进行修改,即实现...
    数独题阅读 2,350评论 0 22
  • **** AOP 面向切面编程 底层原理 代理!!! 今天AOP课程1、 Spring 传统 AOP2、 Spri...
    luweicheng24阅读 1,405评论 0 1
  • 这是我在华珠导图班结束后,创作的第二幅导图——《华珠导图认证班知识整理》,第一感觉就是线条好生硬,导图布局不美观。...
    王子市阅读 358评论 0 0