You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非Spring Boot的传统Spring项目集成Redis缓存遇类缺失问题求助

在纯Spring(非Spring Boot)项目中集成Redis缓存的解决方案

错误原因

你引入的是Spring Boot专属的starter依赖,这类依赖会自动关联Boot核心组件及高版本Spring类库,但你的传统Spring项目版本较低,缺少org.springframework.core.metrics.ApplicationStartUp这类仅在高版本Spring/Boot中存在的类,因此启动报错。必须替换为适配纯Spring项目的基础依赖。

纯Spring集成Redis缓存步骤

1. 替换依赖(以Maven为例)

移除原有的Boot starter依赖,添加以下纯Spring与Redis相关依赖,版本需与你项目中现有Spring版本匹配:

<!-- Spring核心上下文(缓存注解基础) -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>你的Spring版本,如5.2.19.RELEASE</version>
</dependency>

<!-- Spring Data Redis(Redis操作封装) -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>对应Spring版本的适配版,如2.3.12.RELEASE</version>
</dependency>

<!-- Redis客户端(二选一:Jedis或Lettuce) -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.7.0</version>
</dependency>

<!-- 若你的Spring版本为4.x,需额外添加 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>你的Spring版本</version>
</dependency>

2. 配置Redis与缓存

根据项目配置方式(XML或Java Config)选择对应配置:

方式一:XML配置

<!-- 启用缓存注解支持 -->
<cache:annotation-driven />

<!-- Redis连接工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="localhost" />
    <property name="port" value="6379" />
    <!-- 若Redis设置密码,取消注释 -->
    <!-- <property name="password" value="your-redis-password" /> -->
    <property name="database" value="0" />
</bean>

<!-- Redis模板(封装Redis操作) -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory" />
    <!-- 配置序列化器,避免默认JDK序列化的兼容性问题 -->
    <property name="keySerializer">
        <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    </property>
    <property name="valueSerializer">
        <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
    </property>
</bean>

<!-- Redis缓存管理器 -->
<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
    <constructor-arg name="redisOperations" ref="redisTemplate" />
    <!-- 默认缓存过期时间(单位:秒) -->
    <property name="defaultExpiration" value="1800" />
    <!-- 为特定缓存设置单独过期时间 -->
    <property name="expires">
        <map>
            <entry key="userCache" value="3600" /> <!-- 1小时 -->
        </map>
    </property>
</bean>

<!-- 指定使用Redis缓存管理器 -->
<cache:annotation-driven cache-manager="redisCacheManager" />

方式二:Java Config配置

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;

@Configuration
@EnableCaching
public class RedisCacheConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName("localhost");
        factory.setPort(6379);
        // factory.setPassword("your-redis-password");
        factory.setDatabase(0);
        return factory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }

    @Bean
    public RedisCacheManager redisCacheManager() {
        return RedisCacheManager.builder(redisTemplate())
                // 默认缓存过期时间:30分钟
                .cacheDefaults(org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig()
                        .entryTtl(Duration.ofMinutes(30)))
                // 为userCache设置1小时过期时间
                .withCacheConfiguration("userCache",
                        org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig()
                                .entryTtl(Duration.ofHours(1)))
                .build();
    }
}

3. 使用缓存注解

在业务Service方法上添加缓存注解,实现缓存逻辑:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    // 将查询结果缓存到userCache中,key为userId
    @Cacheable(value = "userCache", key = "#userId")
    public User getUserById(Long userId) {
        // 此处为数据库查询逻辑
        return userDao.findById(userId);
    }
}

内容的提问来源于stack exchange,提问作者CR Sardar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 06:01:04