@CachePut注解中参数对象在SpEL键表达式中莫名为null的问题求助
@CachePut注解中参数对象在SpEL键表达式中莫名为null的问题求助
我遇到了一个非常诡异的Spring缓存问题,实在搞不懂哪里出了问题,想请教一下大家!
我现在实现的功能是从数据库读取统计数据,然后把数据加载到缓存里。明明日志里清清楚楚显示传入方法的Statistic对象非空,还能正常打印它的type和location,但@CachePut注解里的SpEL键表达式却一直报对象为null的错误,找不到type属性。
相关代码
首先是预加载缓存的方法:
public void preloadCache() { List<Statistic> allData = repository.findAll(); log.info("Data found {}", allData.size()); allData.forEach(cacheService::populateQuarterStatisticCache); }
然后是处理缓存的CacheService类:
@Service @Slf4j public class CacheService { @CachePut(value = "quarterStatistic", key = "#statistic.type + '-' + #statistic.location") public QuarterBusinessesStatistic populateQuarterStatisticCache(Statistic statistic) { log.info("Loading statistic into cache: {}-{}", statistic.getType(), statistic.getLocation()); return statistic.getData(); } }
日志信息
从日志能看到,数据库查到了1条数据,而且传入CacheService的对象确实非空,还能打印出restaurant-New York:
[nio-8080-exec-1] StatisticClient : Data found 1 [nio-8080-exec-1] CacheService : Loading statistic into cache: restaurant-New York
但紧接着就弹出了这个错误:
SpelEvaluationException: EL1007E: Property or field 'type' cannot be found on null
我尝试过的解决方法
- 把SpEL键表达式改成空安全的写法:
key = "#statistic?.type + '-' + #statistic?.location"- 结果:不再报错,但缓存里的键变成了
null-null,完全不是我想要的restaurant-New York
- 结果:不再报错,但缓存里的键变成了
- 尝试用方法调用代替属性访问:
key = "#statistic.getType() + '-' + #statistic.getLocation()"- 结果:还是报同样的对象为null的错误
我的环境配置
pom.xml的关键配置如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>MyService</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>21</java.version> <spring-boot.version>3.3.5</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
有没有人遇到过类似的问题?或者能帮我分析一下为什么会出现这种日志和代码表现完全矛盾的情况吗?
备注:内容来源于stack exchange,提问作者Dmytro Guriev
相关产品推荐
相关产品推荐

