如题,主要原因是由于过滤器类对象的构建顺序先于对象注入,导致过滤器中各类使用Springboot注入的对象均为空对象
尝试各种方法,未果
最终,自定义一个Redis工具类加入Springboot管理,同时工具类实现Springboot感知接口,通过Context应用上下文拿到IOC容器中RedisTemplate bean对象,随后在过滤器中直接使用RedisUtils解决问题
package com.haiyisoft.hsys.framework.factory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; //import javax.annotation.PostConstruct;(该方法不可用) //import javax.annotation.Resource; /** * 感知接口拿到IOC容器中bean对象 */ @Component public class RedisUtils implements ApplicationContextAware { //赋值一个静态的redisTemplate public static RedisTemplate redisTemplate; //感知类获取Bean对象 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { redisTemplate = applicationContext.getBean("redisTemplate", RedisTemplate.class);//指定所需bean对象名称和类对象 } public void redisTemplate() { redisTemplate = this.redisTemplate; } }