Java 基础教程

Java 面向对象

Java 高级教程

Java 笔记

Java FAQ

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/java-redis-template-not-initialized-error.html

java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using it 错误原因及解决方法

Java 笔记 Java 笔记


spring 容器初始化 RedisTemplate 并调用 RedisTemplate 的方法时,报错 java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using it

报错信息

报错的详细信息如下:

java.lang.IllegalArgumentException: template not initialized; call afterPropertiesSet() before using it
	at org.springframework.util.Assert.isTrue(Assert.java:116)
	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:200)
	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
	at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
	at org.springframework.data.redis.core.DefaultHashOperations.entries(DefaultHashOperations.java:231)
    ......

解决方法

RedisTemplate 不能显性直接使用,需要在 spring 容器里实例化,通过 spring 启动程序并注入 RedisTemplate 相应依赖 bean。