Spring boot webSocket整合redis过程中,发现注入不了redis,redis注入失败

1:通过sping上下文的容器来取得:

Spring boot webSocket整合redis过程中,发现注入不了redis,redis注入失败

Spring boot webSocket整合redis过程中,发现注入不了redis,redis注入失败

2:配置多个redis的实例

package com.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.collections.RedisProperties;

import java.net.UnknownHostException;

/**
 * 
 **/
@Configuration
@ConditionalOnClass(RedisOperations.class)
//@EnableConfigurationProperties(RedisProperties.class)
//@Import({LettuceClientConfiguration.class, JedisClientConfiguration.class})
public class RedisAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(

            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

        RedisTemplate<Object, Object> template = new RedisTemplate<>();

        template.setConnectionFactory(redisConnectionFactory);

        return template;

    }


    @Bean

    @ConditionalOnMissingBean

    public StringRedisTemplate stringRedisTemplate(

            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

        StringRedisTemplate template = new StringRedisTemplate();

        template.setConnectionFactory(redisConnectionFactory);

        return template;

    }
}

3:上下文工具类

package com.abao.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Repository;

/**
 
 **/
@Repository
public class SpringUtils implements BeanFactoryPostProcessor {

    //Spring应用上下文环境
    private static ConfigurableListableBeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    public static ConfigurableListableBeanFactory getBeanFactory() {
        return beanFactory;
    }

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws org.springframework.beans.BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        return (T) getBeanFactory().getBean(name);
    }

    /**
     * 获取类型为requiredType的对象
     *
     * @param clz
     * @return
     * @throws org.springframework.beans.BeansException
     *
     */
    public static <T> T getBean(Class<T> clz) throws BeansException {
        T result = (T) getBeanFactory().getBean(clz);
        return result;
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
     *
     * @param name
     * @return boolean
     */
    public static boolean containsBean(String name) {
        return getBeanFactory().containsBean(name);
    }

    /**
     * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
     *
     * @param name
     * @return boolean
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().isSingleton(name);
    }

    /**
     * @param name
     * @return Class 注册对象的类型
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().getType(name);
    }

    /**
     * 如果给定的bean名字在bean定义中有别名,则返回这些别名
     *
     * @param name
     * @return
     * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
     *
     */
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return getBeanFactory().getAliases(name);
    }
}


4:redis 工具类

 

package com.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 
 **/
public class RedisTools {

    public static <T> List<T> hget(StringRedisTemplate stringRedisTemplate,String key,Class<T> clazz){
        List<Object> list = stringRedisTemplate.opsForHash().values(key);
        return JSON.parseArray(list.toString(), clazz);
    }

    public static <T> T hget(StringRedisTemplate stringRedisTemplate,String h,String key,Class<T> clazz){
        Object o = stringRedisTemplate.opsForHash().get(h,key);
        return JSON.parseObject(o.toString(), clazz);
    }

    public static void hset(StringRedisTemplate stringRedisTemplate,String h,String key,String value){
        stringRedisTemplate.opsForHash().put(h,key,value);
    }

    public static void expire(StringRedisTemplate stringRedisTemplate,String key,Integer time){
        stringRedisTemplate.expire(key,time, TimeUnit.SECONDS);
    }

    public static Boolean existsHash(StringRedisTemplate stringRedisTemplate,String h,String key){
        return stringRedisTemplate.opsForHash().hasKey(h,key);
    }

    public static Boolean existsValue(StringRedisTemplate stringRedisTemplate,String key){
        return stringRedisTemplate.hasKey(key);
    }

    public static Long hdel(StringRedisTemplate stringRedisTemplate,String h,String key){
        return stringRedisTemplate.opsForHash().delete(h,key);
    }

    public static void set(StringRedisTemplate stringRedisTemplate,String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
        expire(stringRedisTemplate,key,60*60);
    }

    public static <T> T get(StringRedisTemplate stringRedisTemplate,String key,Class<T> clazz){
        Object object = stringRedisTemplate.opsForValue().get(key);
        return JSON.parseObject(object.toString(), clazz);
    }

    public static Object get(StringRedisTemplate stringRedisTemplate,String key){
        Object object = stringRedisTemplate.opsForValue().get(key);
        return object;
    }
}