使用Maven添加Redis依赖(如何添加redis依赖包)
RecentYears,Redis has become one of the most popular nosql databases, and almost everywhere you can see it’s shadow. With the increasing popularity of Redis and its application. whether it’s a Java development project or a spring boot project, we should add a Redis dependency.Among them, there are various methods added sush as maven. The following is a tutorial on how to add a Redis dependency through maven.
First, open the `pom.xml` in the project, add the followingRedis dependency:
“` xml
org.springframework.boot
spring-boot-starter-data-redis
2.3.3.RELEASE
Or
``` xml
org.springframework.data spring-data-redis
2.3.1.RELEASE
Then, edit the storage address and password in the configuration item such as resource.properties , and finally start RedisConfig.java at the startup. The connection and password can be configured as follows:
“`properties
#redis配置文件
spring.redis.host=10.0.0.1
spring.redis.port=6379
spring.redis.timeout=3000
spring.redis.password=123456
```java@Configuration
@EnableCachingpublic class RedisConfig {
@Bean
@ConfigurationProperties(prefix="spring.redis") public JedisPoolConfig getJedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); return jedisPoolConfig;
}
@Bean @ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getJedisConnectionFactory(JedisPoolConfig config){ JedisConnectionFactory factory = new JedisConnectionFactory();
JedisPoolConfig config1 = getJedisPoolConfig(); factory.setPoolConfig(config1);
return factory; }
@Bean
public RedisTemplate getRedisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate;
}
}
Finally, the dependency library Redis is added to maven through these steps.In addition, we can find that when we add the Redis dependency, there are many versions of the Redis dependency library. It is recommended to choose the version that has been tested and proven to be stable.