zoukankan      html  css  js  c++  java
  • spring boot 使用拦截器 无法注入 配置值 、bean问题

    参考:https://www.cnblogs.com/SimonHu1993/p/8360701.html

    一定要将 拦截器组件 交给spring容器进行管理,这样才能注入配置值,或者注入bean:

    package com.thunisoft.maybeemanagementcenter.interceptor;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class WebAppConfigure extends WebMvcConfigurerAdapter {
    
        @Value("${thunisoft.microservice.mvc.interceptor.includePath:/**}")
        private String includePath;
        @Value("${thunisoft.microservice.mvc.interceptor.excludePath:/login/*}")
        private String excludePath;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            String[] excludePaths = excludePath.split(",");
            String[] includePaths = includePath.split(",");
            InterceptorRegistration ir = registry.addInterceptor(loginInterceptor())
                    .addPathPatterns(includePaths)
                    .excludePathPatterns(excludePaths);
            super.addInterceptors(registry);
        }
    
        @Bean
        public LoginInterceptor loginInterceptor() {
            return new LoginInterceptor();
        }
    }
    

     

    重点代码:

    @Bean
        public LoginInterceptor loginInterceptor() {
            return new LoginInterceptor();
        }
    
    registry.addInterceptor(loginInterceptor())
    
  • 相关阅读:
    java数据类型
    如何判断数组
    git 常用命令
    如何配置 ESLint 工作流
    Lambda表达式和函数式接口
    面向对象(多态与内部类)
    面向对象(封装与继承)
    面相对像(基础)
    break;怎么跳出外部循环
    面向对象(类与对象)
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/8857185.html
Copyright © 2011-2022 走看看