SpringBoot - HandlerInterceptor 中 @Autowired 为空

时间:2021-08-21 20:03:01   收藏:0   阅读:16

前言

public class PermissionInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService; // null
    
}

解决方法

@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PermissionInterceptor());
    }
}
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {

    @Bean
    public PermissionInterceptor getPermissionInterceptor() {
        return new PermissionInterceptor();
    }

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

- End -
﹀
﹀
﹀
梦想是咸鱼
关注一下吧
技术分享图片

原文:https://www.cnblogs.com/maggieq8324/p/15169518.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!