> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/30.-working-with-nosql-technologies/30.1.-redis/30.1.1.-connecting-to-redis.md).

# 30.1.1. 连接Redis

你可以注入一个自动配置的`RedisConnectionFactory`，`StringRedisTemplate`或普通的`RedisTemplate`实例，或任何其他Spring Bean只要你愿意。默认情况下，这个实例将尝试使用`localhost:6379`连接Redis服务器。下面的列表展示了这样的bean的一个示例：

```java
@Component
public class MyBean {

    private StringRedisTemplate template;

    @Autowired
    public MyBean(StringRedisTemplate template) {
        this.template = template;
    }
    // ...
}
```

**提示** 为了实现更高级的定制，你也可以注册任意数量的实现了`LettuceClientConfigurationBuilderCustomizer`的bean。如果你使用Jedis，那就实现`JedisClientConfigurationBuilderCustomizer`。

如果你添加一个自己的，或任何自动配置类型的`@Bean`，它将替换默认实例（除了`RedisTemplate`的情况，它是根据`bean`的name 'redisTemplate'而不是类型进行排除的）。如果在classpath路径下存在`commons-pool2`，默认你会获得一个连接池工厂。
