解决mybatis拦截器注入依赖后为null问题
调试时发现在mybatis拦截器定义的注解@PostConstruct方法中RedisUtils依赖已经注入,但是在拦截器的Intercept方法中redisUtils为null。在配置类中将MybatisInterceptor改成@Bean注入后,Intercept执行时依赖问题解决。需求是在mybatis拦截器中注入RedisUtils用来缓存一些信息。
需求是在mybatis拦截器中注入RedisUtils用来缓存一些信息。
拦截器
@Component
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
public class MybatisInterceptor implements Interceptor {
private static final String CONFIG_KEY = "config";
private static Map<String, Map<String, Config>> configMap;
@Autowired
private RedisUtils redisUtils;
@PostConstruct
public void init() {
initializeMap();
}
private synchronized void initializeMap() {
String ConfigStr = redisUtils.getValue(CONFIG_KEY);
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
String ConfigStr = redisUtils.getValue(CONFIG_KEY);
...
}
...
}
配置类
@Configuration
@AutoConfigureAfter({PageHelperAutoConfiguration.class})
public class MybatisInterceptorConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
public MybatisInterceptorConfig() {
}
@PostConstruct
public void addMyInterceptor() {
MybatisInterceptor mybatisInterceptor = new MybatisInterceptor();
Iterator iterator = this.sqlSessionFactoryList.iterator();
while(iterator .hasNext()) {
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)var2.next();
sqlSessionFactory.getConfiguration().addInterceptor(mybatisInterceptor);
}
}
}
调试时发现在mybatis拦截器定义的注解@PostConstruct方法中RedisUtils依赖已经注入,但是在拦截器的Intercept方法中redisUtils为null。
在配置类中将MybatisInterceptor改成@Bean注入后,Intercept执行时依赖问题解决
@Configuration
@AutoConfigureAfter({PageHelperAutoConfiguration.class})
public class MybatisInterceptorConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@Bean
public MybatisInterceptor mybatisInterceptor() {
return new MybatisInterceptor();
}
@PostConstruct
public void addMyInterceptor() {
MybatisInterceptor mybatisInterceptor = mybatisInterceptor();
Iterator iterator = this.sqlSessionFactoryList.iterator();
while(iterator .hasNext()) {
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)var2.next();
sqlSessionFactory.getConfiguration().addInterceptor(mybatisInterceptor);
}
}
}
其原因个人推测是通过@Component注入虽然可以将依赖注入到spring中,但是SqlSessionFactory.getConfiguration()中的拦截器是通过new设置的,在执行@PostConstruct初始化方法时用的时spring中注入的MybatisInterceptor,而在执行Intercept拦截时用的时new出的实例方法。因此将配置类中的MybatisInterceptor bean设置到SqlSessionFactory中即可。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)