1. 程式人生 > >Maven整合SSM和Redis,親測

Maven整合SSM和Redis,親測

<?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:p="http://www.springframework.org/schema/p"

         xmlns:context="http://www.springframework.org/schema/context"

         xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"

         xmlns:tx="http://www.springframework.org/schema/tx"

         xsi:schemaLocation="http://www.springframework.org/schema/beans 

                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 

                        http://www.springframework.org/schema/context 

                        http://www.springframework.org/schema/context/spring-context-4.0.xsd 

                        http://www.springframework.org/schema/mvc

                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

                        http://www.springframework.org/schema/aop

                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

                        http://www.springframework.org/schema/tx

                        http://www.springframework.org/schema/tx/spring-tx.xsd">

         <!-- redis資料來源 -->

         <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">

                  <!-- 最大空閒數 -->

                  <property name="maxIdle" value="${redis.maxIdle}" />

                  <!-- 最大空連線數 -->

                  <property name="maxTotal" value="${redis.maxTotal}" />

                  <!-- 最大等待時間 -->

                  <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />

                  <!-- 連線超時時是否阻塞,false時報異常,ture阻塞直到超時, 預設true -->

                  <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" />

                  <!-- 返回連線時,檢測連線是否成功 -->

                  <property name="testOnBorrow" value="${redis.testOnBorrow}" />

         </bean>

         <!-- Spring-redis連線池管理工廠 -->

         <bean id="jedisConnectionFactory"

                  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

                  p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"

                  p:timeout="${redis.timeout}" p:poolConfig-ref="poolConfig" p:usePool="true">

         </bean>

         <!-- redis template definition -->

         <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

                  <property name="connectionFactory" ref="jedisConnectionFactory" />

                  <property name="keySerializer">

                          <bean

                                   class="org.springframework.data.redis.serializer.StringRedisSerializer" />

                  </property>

                  <property name="valueSerializer">

                          <bean

                                   class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

                  </property>

                  <property name="hashKeySerializer">

                          <bean

                                   class="org.springframework.data.redis.serializer.StringRedisSerializer" />

                  </property>

                  <property name="hashValueSerializer">

                          <bean

                                   class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

                  </property>

                  <!--開啟事務 -->

                  <property name="enableTransactionSupport" value="true"></property>

         </bean>

         <!-- 快取攔截器配置 -->

         <bean id="methodCacheInterceptor" class="com.ivan.redis.MethodCacheInterceptor">

                  <property name="redisUtil" ref="redisUtil" />

                  <property name="defaultCacheExpireTime" value="${defaultCacheExpireTime}" />

                  <!-- 禁用快取的類名列表 -->

                  <property name="targetNamesList">

                          <list>

                                   <value></value>

                          </list>

                  </property>

                  <!-- 禁用快取的方法名列表 -->

                  <property name="methodNamesList">

                          <list>

                                   <value>add</value>

                                   <value>delete</value>

                                   <value>edit</value>

                          </list>

                  </property>

         </bean>

         <bean id="redisUtil" class="com.ivan.redis.RedisUtil">

                  <property name="redisTemplate" ref="redisTemplate" />

         </bean>

         <!--配置切面攔截方法 -->

         <aop:config proxy-target-class="true">

         <!-- <aop:pointcut id="controllerMethodPointcut"

                          expression="execution(* com.ivan.service.impl.*.get*(..))" /> -->

                  <aop:pointcut id="controllerMethodPointcut"

                          expression="execution(* com.ivan.service.impl.*.*(..))" />

                  <aop:advisor advice-ref="methodCacheInterceptor"

                          pointcut-ref="controllerMethodPointcut" />

         </aop:config>

</beans>