Spring 整合 SpringDataRedis 女爷i 2022-12-13 04:34 129阅读 0赞 ## 1.1 相关依赖 ## <!-- jedis 2.9.0 会报 ClassNotFoundException: redis.clients.jedis.util.Pool --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.3.4.RELEASE</version> </dependency> ## 1.2 配置文件 ## <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置信息 --> <context:property-placeholder location="classpath:*.properties" /> <!-- redis 参数(不配置为默认) --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- redis 地址配置,无密码可以省略 --> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <!-- redisTemplate 配置 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory" /> </bean> </beans> ## 1.3 示例 ## /** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/10/10 * @description Spring 整合 Spring Data Redis */ @SpringJUnitConfig(locations = "classpath:redis.xml") public class RedisTest { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test public void redis() { redisTemplate.opsForValue().set("name", "张三"); Object name = redisTemplate.opsForValue().get("name"); System.out.println(name); } } ![在这里插入图片描述][20201010112818438.png] [20201010112818438.png]: /images/20221123/8fb8dcae95a34132ab76b6ce0885ac92.png
还没有评论,来说两句吧...