spring redis 集成 详细步骤
2017-10-13 15:15
183 查看
1 添加jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.1</version>
</dependency>
<!-- redis 必须在引入Spring包之后 否则会报错-->
<!-- redis 注:Spring-core-4.1.0后可无顺序,Spring-core-4.1.0必须在引入Spring包之后 -->
2
在redis.properties配置文件中配置好下面的属性
example:
# Redis settings
#redis.host=1xx.xx.xx.xxx
#redis.port=6379
#redis.pass=foobared
redis.host=127.0.0.1
redis.port=6379
redis.pass=xxxx
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
3 applicationContect.xml文件配置中添加
<context:property-placeholder location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
4 spring对dao层的封装类似于 模板方式。
RedisTemplate就是spring对Redis的一个封装
RedisTemplate<String, Object> redis;
redis.opsForHash();
redis.opsForList();
redis.opsForSet();
redis.opsForValue();
redis.opsForZSet();
redis.opsForHyperLogLog();
其它详细的操作都是基于上面封装的操作进行的
<!-- Redis连接池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="600"></property>
<property name="minIdle" value="2"></property>
<property name="maxWaitMillis" value="15000"></property>
<property name="minEvictableIdleTimeMillis" value="100"></property>
<property name="numTestsPerEvictionRun" value="100"></property>
<property name="timeBetweenEvictionRunsMillis" value="1000"></property>
<property name="testOnBorrow" value="true"></property>
<property name="testOnReturn" value="true"></property>
<property name="testWhileIdle" value="true"></property>
</bean>
<!-- Redis连接 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.password}" /> -->
<property name="timeout" value="15000" />
<property name="database" value="0" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- 对string操作的封装 -->
<bean id="redis" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory" p:enableTransactionSupport="false" >
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.1</version>
</dependency>
<!-- redis 必须在引入Spring包之后 否则会报错-->
<!-- redis 注:Spring-core-4.1.0后可无顺序,Spring-core-4.1.0必须在引入Spring包之后 -->
2
在redis.properties配置文件中配置好下面的属性
example:
# Redis settings
#redis.host=1xx.xx.xx.xxx
#redis.port=6379
#redis.pass=foobared
redis.host=127.0.0.1
redis.port=6379
redis.pass=xxxx
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
3 applicationContect.xml文件配置中添加
<context:property-placeholder location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
4 spring对dao层的封装类似于 模板方式。
RedisTemplate就是spring对Redis的一个封装
RedisTemplate<String, Object> redis;
redis.opsForHash();
redis.opsForList();
redis.opsForSet();
redis.opsForValue();
redis.opsForZSet();
redis.opsForHyperLogLog();
其它详细的操作都是基于上面封装的操作进行的
<!-- Redis连接池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="600"></property>
<property name="minIdle" value="2"></property>
<property name="maxWaitMillis" value="15000"></property>
<property name="minEvictableIdleTimeMillis" value="100"></property>
<property name="numTestsPerEvictionRun" value="100"></property>
<property name="timeBetweenEvictionRunsMillis" value="1000"></property>
<property name="testOnBorrow" value="true"></property>
<property name="testOnReturn" value="true"></property>
<property name="testWhileIdle" value="true"></property>
</bean>
<!-- Redis连接 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.password}" /> -->
<property name="timeout" value="15000" />
<property name="database" value="0" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- 对string操作的封装 -->
<bean id="redis" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory" p:enableTransactionSupport="false" >
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
相关文章推荐
- 集成微信支付详细步骤及说明——解决点击微信支付只有一个确定按钮的情况
- 微信Tinker热修补的快速集成详细步骤
- android APP集成系统详细步骤及注意事项(amlogic平台)
- Android studio Android集成百度身份证识别 详细步骤
- iOS开发融云即时通讯集成详细步骤
- iOS开发融云即时通讯集成详细步骤
- Spring-Boot 集成Solr客户端的详细步骤
- SVN服务器端、客户端安装以及集成到eclipse的详细步骤
- iOS开发融云即时通讯集成详细步骤
- iOS百度地图SDK集成详细步骤
- SVN服务器端、客户端安装以及集成到eclipse的详细步骤(转)
- SVN服务器端、客户端安装以及集成到eclipse的详细步骤
- Cocos2d-x 3.0中集成社交分享ShareSDK的详细步骤和常见问题解决
- 记录sprint boot 集成mybatis 注解,多数据源配置项目搭建,非常详细,只要按照步骤来,项目绝对能跑起来!!!
- 【ELK】自定义ES-5.4.1插件步骤2——带action-&amp;gt;transportaction分层结构并包含详细的集成测试步骤
- Red5-1.0-Final集成到tomcat详细步骤
- iOS开发融云即时通讯集成详细步骤(蔡哲永)
- shiro和spring的集成(详细步骤)
- 亚马逊SNS集成GCM的详细步骤
- BIEE Mobile App 集成应用详细步骤说明