设为首页 收藏本站
查看: 2117|回复: 0

[经验分享] spring+springmvc+mybatis+redis 实现两重数据缓存

[复制链接]

尚未签到

发表于 2017-12-21 14:35:52 | 显示全部楼层 |阅读模式
<?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:lang="http://www.springframework.org/schema/lang"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:cache="http://www.springframework.org/schema/cache"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/lang
  http://www.springframework.org/schema/lang/spring-lang.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/cache
  http://www.springframework.org/schema/cache/spring-cache.xsd">
  <!-- 包扫描 -->
  <context:component-scan base-package="sysone.zr.com" />
  <!-- 引入外部文件 -->
  <context:property-placeholder location="/WEB-INF/config/jdbc.properties,/WEB-INF/config/redis.properties"/>
  <!-- 开启动态代理 -->
  <aop:aspectj-autoproxy/>
  <!-- 事务属性配置 -->
  <tx:advice transaction-manager="txManager">
  <!-- 事务传播属性 -->
  <tx:attributes>
  <!-- 所有已get、query、select开头的方法都是只读 -->
  <tx:method name="get*" read-only="true"/>
  <tx:method name="query*" read-only="true"/>
  <tx:method name="select*" read-only="true"/>
  <!-- 其它的所有方法支持事务设置的属性(异常回滚) -->
  <tx:method name="*" rollback-for="java.lang.Throwing" />
  </tx:attributes>
  </tx:advice>
  <aop:config>
  <!-- 配置切点 -->
  <aop:pointcut expression="execution(* sysone.zr.com.service.sysone.zr.com.service.impl.*.*(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
  </aop:config>
  <!-- 数据源配置 -->   
  <bean >
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
  </bean>
  <!-- 配置数据源管理器 -->
  <bean>
  <property name="dataSource" ref="dataSource"/>
  </bean>
  <!-- mybatis配置 -->
  <bean>
  <property name="dataSource" ref="dataSource"/>
  <property name="configLocation" value="/WEB-INF/config/mybatis-config.xml"/>
  <property name="typeAliasesPackage" value="sysone.zr.com.mapper.model"/>
  <property name="mapperLocations">
  <list>
  <value>classpath:sysone/zr/com/mapper/xml/*Mapper.xml</value>
  </list>
  </property>
  </bean>
  <!-- mybatis 扫描 -->
  <bean>
  <property name="basePackage" value="sysone.zr.com.mapper.imapper"/>
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  </bean>
  <!-- 配置redis 单机版 -->
  <!-- redis数据源 -->
  <bean>  
  <!-- 最大空闲数 -->
  <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>
  <!-- Spring-redis连接池管理工厂 -->
  <bean>
  <!-- IP地址 -->
  <property name="hostName" value="${redis.host}" />
  <!-- 端口号 -->
  <property name="port" value="${redis.port}" />
  <!-- 超时时间 -->
  <property name="timeout" value="${redis.timeout}" />
  <property name="poolConfig" ref="poolConfig" />
  </bean>
  <!-- redis 集群 -->
  <!--  <bean>
  <constructor-arg name="nodes">
  <bean>
  <constructor-arg name="host" value="192.168.10.105"></constructor-arg>
  <constructor-arg name="port" value="7002"></constructor-arg>
  </bean>
  <bean>
  <constructor-arg name="host" value="192.168.10.77"></constructor-arg>
  <constructor-arg name="port" value="7002"></constructor-arg>
  </bean>
  </constructor-arg>
  </bean>  -->
  <!-- redis模板类,提供了对缓存的增删改查 -->
  <bean>
  <property name="connectionFactory" ref="jedisConnectionFactory" />
  <property name="keySerializer">
  <bean />
  </property>
  <property name="valueSerializer">
  <bean />
  </property>
  </bean>
  <!-- StrRedisTemplate -->
  <bean>
  <property name="connectionFactory" ref="jedisConnectionFactory" />
  <property name="keySerializer">
  <bean />
  </property>
  <property name="valueSerializer">
  <bean />
  </property>
  <property name="hashKeySerializer">
  <bean />
  </property>
  </bean>
  <!-- 使用中间类解决RedisCache.jedisConnectionFactory的静态注入,从而使MyBatis实现第三方缓存 -->
  <bean>
  <property name="jedisConnectionFactory" ref="jedisConnectionFactory"/>
  </bean>
  <!-- //End 单机版Redis集成 -->
  <!-- Redis缓存管理对象 -->
  <!-- <bean>
  <constructor-arg index="0" ref="redisTemplate" />
  </bean> -->
  <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->  
  <bean>  
  <property name="caches">  
  <set>  
  <bean>  
  <property name="redisTemplate" ref="redisTemplate" />  
  <property name="name" value="lf_cache"/>  
  </bean>  
  </set>  
  </property>  
  </bean>  
  

  <!-- 开启Spring缓存 -->
  <cache:annotation-driven cache-manager="cacheManager"/>
  <!-- Spring容器实例 -->
  <!-- <bean /> -->
  
</beans>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426481-1-1.html 上篇帖子: C#Redis Sorted-Sets 下篇帖子: Springboot使用redis
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表