spring boot 整合redis

引入pom 依赖

org.springframework.boot
spring-boot-starter


org.springframework.boot
spring-boot-starter-data-redis

redis 配置类
import com.dxy.platform.scheduler.Receiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**

  • redis配置类

  • @author zxy
    **/
    @Configuration
    public class RedisConfig {
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
    MessageListenerAdapter listenerAdapter) {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    //redis做消息列队监听的通道
    container.addMessageListener(listenerAdapter, new PatternTopic("webpush"));
    return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
    //监听处理bean 和处理方法
    return new MessageListenerAdapter(receiver, "receiveMessage");
    }
    //监听处理bean
    @Bean
    Receiver receiver() {
    return new Receiver();
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
    return new StringRedisTemplate(connectionFactory);
    }
    }

你可能感兴趣的:(spring boot 整合redis)