Spring Cache编写实例

首先我们来看一下如何使用spring3.1自己的cache,
需要在命名空间中增加cache的配置

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	 xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
			http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">


然后加入申明处理

       <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
	<cache:annotation-driven cache-manager="cacheManager"/>


	<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
	<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
		<property name="caches">
			<set>
				<bean
					class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
					p:name="default" />
				<bean
					class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
					p:name="andCache" />
			</set>
		</property>
	</bean> 


然后在Dao类中加入缓存方法  andCache是存储的地方 eventi是Key值用于寻找存储实例

 @Cacheable(value = "andCache", key = "#eventid")
	  public XX getCache(String eventid)
	  {
		  System.out.println("进来了");
		  return this.get(eventid);
	  }

调用getCache方法的时候它会去andCache这个地方寻找有没有以Key为主键的缓存,如果有则不调用方法,没有则调用该方法。

本人测试过程中发现转换错误:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy11 implementing XX,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [XX] for prop


解决方法是在AOP的配置中加入注入策略方式:

<tx:advice id="transactionInterceptor" transaction-manager="hibernateManager">
		<tx:attributes>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" timeout="300" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" timeout="300" />
			<tx:method name="is*" propagation="SUPPORTS" read-only="true" timeout="300" />
			<tx:method name="batch*" propagation="REQUIRED" isolation="READ_COMMITTED" timeout="1800" rollback-for="Throwable" />
			<tx:method name="audit*" propagation="REQUIRES_NEW" isolation="READ_COMMITTED" timeout="600" rollback-for="Throwable" />
			<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED" timeout="300" rollback-for="Throwable" />
		</tx:attributes>
	</tx:advice>
	<aop:config proxy-target-class="true">
		<aop:advisor advice-ref="transactionInterceptor" pointcut="execution(* com.jasgroup..*.service.impl..*.*(..))" order="40" />
		<aop:advisor advice-ref="transactionInterceptor" pointcut="execution(* cn.jasgroup..*.service.impl..*.*(..))" order="41" />
		<aop:advisor advice-ref="transactionInterceptor" pointcut="execution(* cn.jasgroup..*.services.impl..*.*(..))" order="41" />
	</aop:config>


在aop:config标签中加入proxy-target-class="true"这句话就能解决。同时需要cglib.jar文件。

注意:proxy-target-class属性值决定是 基于接口的还是 基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK基于接口的代理。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值