spring RedisTemplate的使用(一)--xml配置或JavaConfig配置
1、xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <bean id="propertyConfigurer" class="utils.XmlPropertyPlaceholderSupport">
<property name="location" value="classpath:redis.properties" />
</bean> <!-- redis pool -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <!-- redis pool config -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean> <!--redis redisTemplate -->
<bean id="redisTemplate" class="utils.RedisHelper">
<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.StringRedisSerializer" />
</property> <property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property> <property name="enableTransactionSupport" value="true"></property>
</bean> <bean id="redisMessageListener" class="service.RedisSub">
<property name="redisTemplate" ref="redisTemplate" />
</bean> <bean id="redisMessageListenerContainer"
class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
destroy-method="destroy">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="taskExecutor">
<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="10" />
</bean>
</property> <property name="messageListeners">
<map>
<entry key-ref="redisMessageListener">
<list>
<bean
class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="cfd_md" />
</bean>
<!-- <bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="chat*" /> </bean> -->
</list>
</entry>
</map>
</property>
</bean>
</beans>
redis.properties
redis.hostpro=r-3ns15cdbd110c9c4.redis.rds.aliyuncs.com
redis.portpro=6379
redis.passwordpro=abc redis.hostbeta=127.0.0.1
redis.portbeta=6379
redis.passwordbeta=abc redis.hostlocal=47.244.48.230
redis.portlocal=6379
redis.passwordlocal=abc redis.timeout=10000
redis.maxIdle=1
redis.maxTotal=5
redis.maxWaitMillis=10000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.testOnBorrow=true
redis.testWhileIdle=true #redis.sentinel.host1=172.20.1.230
#redis.sentinel.port1=26379 #redis.sentinel.host2=172.20.1.231
#redis.sentinel.port2=26379 #redis.sentinel.host3=172.20.1.232
#redis.sentinel.port3=26379
2、JavcConfig配置,使用springboot 2.1.3
package com.oy; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @Configuration
public class RedisConfig extends CachingConfigurerSupport {
public static final Logger log = LoggerFactory.getLogger(RedisConfig.class); @Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer(); // RedisTemplate
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(redisSerializer);
template.setValueSerializer(redisSerializer);
template.setHashKeySerializer(redisSerializer);
template.setHashValueSerializer(redisSerializer); template.afterPropertiesSet();
return template;
} @Bean(destroyMethod = "destroy")
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory,
MessageListener redisMessageListener) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory); ThreadPoolTaskScheduler taskExecutor = new ThreadPoolTaskScheduler();
taskExecutor.setPoolSize(10);
taskExecutor.initialize();
container.setTaskExecutor(taskExecutor); Map<MessageListener, Collection<? extends Topic>> listeners = new HashMap<>();
List<Topic> list = new ArrayList<>();
list.add(new ChannelTopic("cfd_md"));
listeners.put(redisMessageListener, list);
container.setMessageListeners(listeners); return container;
} }
application.properties
#spring.profiles.active=beta
spring.profiles.active=local logging.file=/home/wwwlogs/xxx/log.log #redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=xxx spring.redis.jedis.pool.maxActive=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=0
spring RedisTemplate的使用(一)--xml配置或JavaConfig配置的更多相关文章
- spring bean的介绍以及xml和注解的配置方法
5.Bean 下边我们来了解一下Bean的: Bean的作用域Bean的生命周期Bean的自动装配Resources和ResourceLoader 5.1Bean容器的初始化 Bean容器的初始化 两 ...
- Spring RedisTemplate操作-xml配置(1)
网上没能找到全的spring redistemplate操作例子,故特意化了点时间做了接口调用练习,基本包含了所有redistemplate方法. 该操作例子是个系列,该片为spring xml配置, ...
- Spring学习记录(十三)---基于xml文件配置AOP
上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP 其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行. 例子:这里的例子和上一篇的例子一样.换成xml方式 / ...
- (spring-第2回【IoC基础篇】)Spring的Schema,基于XML的配置
要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的.而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案. 在深入了解之前,必须要先明白几个标 ...
- Spring声明式事务(xml配置事务方式)
Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...
- Spring第二篇和第三篇的补充【JavaConfig配置、c名称空间、装载集合、JavaConfig与XML组合】
前言 在写完Spring第二和第三篇后,去读了Spring In Action这本书-发现有知识点要补充,知识点跨越了第二和第三篇,因此专门再开一篇博文来写- 通过java代码配置bean 由于Spr ...
- 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...
- Spring介绍及配置(XML文件配置和注解配置)
本节内容: Spring介绍 Spring搭建 Spring概念 Spring配置讲解 使用注解配置Spring 一.Spring介绍 1. 什么是Spring Spring是一个开源框架,Sprin ...
- spring 和springmvc 在 web.xml中的配置
(1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...
随机推荐
- 【JavaScript】常用的数据类型的处理方式
写这篇文章的目的,是在学习过程中反复查找如何对这三种数据类型进行转换的方法,所以干脆总结在一起. 一.字符串 0.includes:string.includes(),查找当前string中是否包含某 ...
- 文件 "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ttt.mdf" 已压缩,但未驻留在只读数据库或文件组中。必须将此文件解压缩。 CREATE DATABASE 失败。无法创建列出的某些文件名。请查看相关错误。 (.Net SqlClient Data Provider)
问题: 文件 "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ttt.mdf" 已压缩,但 ...
- Windows system 在python文件操作时的路径表示方法
file_path =(r'i:\vacpy\ch10\pi_digits.txt') #将文件路径存储在变量file_path中with open (file_path) as file_objec ...
- 文件下载后台报错IllegalStateException: getOutputStream() has already been called
java.lang.IllegalStateException: getOutputStream() has already been called <%@page language=" ...
- Log4j介绍与使用
Log4j三大组件 1) 日志记录器Logger负责输出日志信息,并能够对日志信息进行分类筛选,决定哪些日志信息应该被输出,哪些该被忽略.Loggers组件输出日志信息时分为5个级别:DEBUG.IN ...
- 为fastdfs文件服务器新增一个storage
一.前言: 前期,已经搭建好了一套fastdfs文件服务器,一个tracker和一个storage,且部署在同一台服务器上,已经正式投入运行快半年了,1T的空间现在只剩下100G容量了,现在需要扩容, ...
- SNMP理解
前两天项目要求一个附加功能,远程监视服务器的运行状况,要定期监视指定端口,指定业务,还包括服务器的磁盘空间,内存,CPU使用率等等.这头俩事还好说,ping和telnet也就搞定了,实在不行就开个so ...
- Spring:容器基本用法
bean是Spring 最核心的东西,打个比方,假设Spring是一个水桶,那么bean就是水桶里的水,水桶离开水后,就没啥作用了.我们先来看一下bean的定义: public class Perso ...
- WSDL文件
WSDL: <!--一次webservice调用,其实并不是方法调用,而是发送SOAP消息 ,即xml片段--> <!--以上一篇中的wsdl文档为例,这里我将注释写到文档中 --& ...
- PWM_MOTOR_B
port_cfg.h witti: #define PORT_CONFIG_PIN_E0_USAGE PORT_CONFIG_GPIO_OUT magna ...