前提1:都需要引入maven依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

前提2:需要在配置文件设置redis的地址、用户名密码

#redis配置信息
  redis:
      database: 0
      host: 
      password: 
      port: 6379
      timeout: 5000
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0

方式一:作为组件的形式引用

//先定义组件
@Component
public class RedisComponent {

    private Logger logger = LoggerFactory.getLogger(RedisComponent.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static StringRedisTemplate redis;

    //@PostConstruct注解效果:在Bean初始化之后(构造方法和@Autowired之后)执行指定操作。
    @PostConstruct
    public void getRedisTemplate(){
        redis = this.stringRedisTemplate;
        logger.info("初始化-------redisTemplate----");
    }

}
//然后在需要引用的地方调用RedisComponent.redis
@Component
public class HttpClientUtil {

    private StringRedisTemplate stringRedisTemplate = RedisComponent.redis;
    
    //判断是否存在key
	boolean flag = stringRedisTemplate.hasKey(key);
	
	//根据key获取value
	token = stringRedisTemplate.opsForValue().get(key);
	
	//存储key-value
	stringRedisTemplate.opsForValue().set(Standard.TOKEN_DIR + "wiki", data.toString());
}

方式二:设置启动类加载顺序优先注册redis

//设置项目启动需要加载的类
@Order(1)
public class StartupRunner implements CommandLineRunner {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void run(String... args){
        HttpClientUtil.setStringRedisTemplate(stringRedisTemplate);
    }
}
//在项目的启动类中注册定义好的加载类
@SpringBootApplication
public class WikiLdapLoginApplication {

    public static void main(String[] args) {
        SpringApplication.run(WikiLdapLoginApplication.class, args);
    }

    @Bean
    public StartupRunner startupRunner(){
        return new StartupRunner();
    }
}
//在工具类中定义stringRedisTemplate
@Component
public class HttpClientUtil {

    private final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    private StringRedisTemplate stringRedisTemplate;

    public static void setStringRedisTemplate(StringRedisTemplate template) {
        stringRedisTemplate = template;
    }