• HandlerInterceptor里@Autowired对象为空的解决方法


    That's because Spring isn't managing your PagePopulationInterceptor instance. You are creating it yourself in the below (拦截器内使用@Autowired时出现了null,这是由于你的spring对象注入时机在你的拦截器之后了)

    public @Override void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PagePopulationInterceptor());
    }
    

    change that to

    @Bean
    public PagePopulationInterceptor pagePopulationInterceptor() {
        return new PagePopulationInterceptor();
    }
    
    public @Override void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(pagePopulationInterceptor());
    }
    

    in this way, Spring will manage the lifecycle of the PagePopulationInterceptor instance since it's generated from a @Bean method. Spring will scan it for @Autowired targets and inject them.
    This assumes that PagePopulationInterceptor is in a package to be @ComponentScaned.

  • 相关阅读:
    JavaIO学习:字符流
    Java学习:IO流
    Java中的Filter过滤器
    Java学习:File类中的过滤器接口
    Java学习:File类
    Java学习:递归
    多线程简介(全)
    Java学习:Lambda表达式
    Java学习:线程池
    Java学习:线程间通信
  • 原文地址:https://www.cnblogs.com/lori/p/9259864.html
Copyright © 2020-2023  润新知