spring cache 配置使用aspectj模式织入

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>

转载于:https://my.oschina.net/thinwonton/blog/918006

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值