Working with NoSQL technologies
Spring Data has additional projects that will help you access various NoSQL technologies, including:
-
MongoDB
-
Neo4J
-
Elasticsearch
-
Redis
-
GemFire or Geode
-
Cassandra
-
>Couchbase
-
LDAP
Spring Boot provides auto-configuration for Redis, MongoDB, Neo4j, Solr, Elasticsearch, Cassandra, Couchbase, LDAP, and InfluxDB. Additionally, Spring Boot for Apache Geode provides auto-configuration for Apache Geode. You can use other projects, but you will have to configure them yourself.
Redis
Redis is a cache, message broker, and full-featured key-value store. In addition to this, Spring Boot contains basic autoconfiguration for the client libraries Lettuce and Jedis and abstractions provided via Spring Data Redis.
For convenient collection of dependencies, there is a “starter” spring-boot-starter-data-redis
. By
default, it uses the Lettuce library. This
starter pack works with both traditional and reactive applications.
spring-boot-starter-data-redis-reactive
to ensure consistency with other repositories that support reactive programming .
Connecting to Redis
You can implement auto-configurable RedisConnectionFactory
, StringRedisTemplate
, or a
vanilla RedisTemplate
instance, just like any other Spring bean. By default, the instance will try to
connect to the Redis server at localhost:6379
. The following listing shows an example of such a bean:
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private final StringRedisTemplate template;
public MyBean(StringRedisTemplate template) {
this.template = template;
}
// ...
public Boolean someMethod() {
return this.template.hasKey("spring");
}
}
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.stereotype.Component
@Component
class MyBean(private val template: StringRedisTemplate) {
// ...
fun someMethod(): Boolean {
return template.hasKey("spring")
}
}
LettuceClientConfigurationBuilderCustomizer
for more in-depth customization. ClientResources
can also be customized using ClientResourcesBuilderCustomizer
.
If you are using Jedis, JedisClientConfigurationBuilderCustomizer
is also available. Additionally, you
can register a bean like RedisStandaloneConfiguration
, RedisSentinelConfiguration
or
RedisClusterConfiguration
to gain full control over the configuration.
If you add a custom @Bean
of any of the autoconfigured types, it will override the default type (except
in the case of RedisTemplate
, if the exception is based on the bean name, redisTemplate
,
and not on its type).
By default, the pool connection factory is automatically configured if commons-pool2
is in the
classpath.