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

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

要使用 @autowired 自动注入,就需要知道该注解生效的条件

@autowired 合适生效,即什么时候可以使用 @autowired 注解

根据官方描述:You are free to use any of the standard Spring Framework techniques to define your beans and their injected dependencies.

所以,我们只需要将我们的 Interceptor 注册为一个 bean ,就可以正常的使用@autowired注解了

方法多种多样,这里主要说明 springboot 中的使用方法:

a. 方法一:通过给方法添加 @Bean 注解,返回一个 Interceptor 的 Bean,该 Interceptor 就可以正常的使用 @autowired 了

例:

拦截器类:
public class PermissionInterceptor extends HandlerInterceptorAdapter {

@Autowired
private PortalUserService userService;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        //使用 userService

        return true;
    }

}

拦截器配置类:
@Configuration
public class WebMVCConfig extends WebMvcConfigurerAdapter {

    //在此处,将拦截器注册为一个 Bean 
    @Bean
    public PermissionInterceptor permissionInterceptor() {
        return new PermissionInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/v2/api-docs/**");
        super.addInterceptors(registry);
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,你可以使用拦截器(Interceptor)来拦截控制器方法的返回对象,并对其进行处理。拦截器可以在控制器方法执行之后、视图渲染之前对返回对象进行操作。 首先,创建一个实现 HandlerInterceptor 接口的拦截器类,它包含三个方法:preHandle、postHandle 和 afterCompletion。在这些方法中,你可以对返回对象进行处理。 ```java @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; // 返回 true 表示继续执行后续的拦截器或控制器方法,返回 false 表示中断后续的执行 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 在视图渲染之前对返回对象进行处理 if (modelAndView != null) { modelAndView.addObject("customAttribute", "customValue"); } } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 执行一些清理操作 } } ``` 接下来,在配置类(通常使用 @Configuration 注解标记)中注册拦截器。 ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor).addPathPatterns("/**"); } } ``` 在上述示例中,我们创建了一个名为 MyInterceptor 的拦截器,并将其应用于所有请求路径上。需要注意的是,我们使用了 @Autowired 注解将拦截器注入到配置类中。 当控制器方法执行完毕,但视图渲染之前,拦截器的 postHandle 方法将被调用。在该方法中,你可以对返回对象进行处理,例如向 ModelAndView 对象中添加自定义属性。 注意:拦截器可以用于修改返回对象,但不能直接修改返回的数据。如果你需要修改响应数据,可以考虑使用过滤器(Filter)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值