• 请不要在回答技术问题时复制粘贴 AI 生成的内容
qw564518158
V2EX  ›  程序员

Spring boot 2.2 打成 war 包 过滤器或者拦截器 注入失败

  •  
  •   qw564518158 · Jul 10, 2020 · 3547 views
    This topic created in 2311 days ago, the information mentioned may be changed or developed.
    第一次发帖,好像不能上图,就尴尬了。
    因为 jar 包的顺序和 war 的加载顺序不同。

    Filter 里面注入失败。

    解决方法在 init 初始化这里对 Service 进行手动注入

    public class RESTApiAuthenticationFilter implements Filter {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private SignaConfig signaConfig;

    @Override
    public void init(FilterConfig filterConfig) {
    logger.info("RESTApiAuthentication filter has been launched!");
    //手动注入
    BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext());
    signaConfig = factory.getBean(SignaConfig.class);
    apiUserService = factory.getBean(ApiUserService.class);
    }

    @Override
    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse,
    FilterChain chain) throws IOException, ServletException {
    }

    @Override
    public void destroy() {
    // TODO Auto-generated method stub

    }
    }

    在 Filter 初始化的时候手动注入即可。
    这个问题已经馋了我三天三夜了
    14 replies  •  2020-07-15 16:35:53 +08:00
    mosliu
    mosliu
    Jul 10, 2020
    1
    用 @Order(1000 ) 自己定义注入顺序应该就可以了吧
    hfc
    hfc
    Jul 10, 2020
    2
    注入失败有异常抛出嘛?有个 DependsOn 的注解,不知道有没有用。
    xuanbg
    xuanbg
    Jul 10, 2020
    3
    可以曲线救国,在另一个 Abc 类里面注入,这个类写个 @Component 就会被扫到自动实例化。然后在 Filter 里面用 ApplicationContextHolder.getContext().getBean(Abc.class)获取 Abc 的实例。
    Reminders
    Reminders
    Jul 10, 2020 via iPhone
    4
    不懂就问,为啥不用 jar 而是用 war ?
    hantsy
    hantsy
    Jul 10, 2020
    5
    看不出来你的 Filter 是哪里注册的。WebXML ?

    Servlet 中的 Servlet,Filter 等在 Spring Boot 中可以直接声明为 Component/Bean,Spring Boot 会帮你的注册。
    qw564518158
    qw564518158
    OP
    Jul 10, 2020
    6
    @mosliu 还有这等操作?下次试试看
    WhenAtRains
    WhenAtRains
    Jul 10, 2020
    7
    应该是自动注入 @Autowired 的事情
    WhenAtRains
    WhenAtRains
    Jul 10, 2020
    8
    我记得这个错是要看 localhost_accessxxxx 的日志才能在网上找到答案。看 catalina 日志看不出来
    ala2008
    ala2008
    Jul 10, 2020
    9
    是的,上下文不一样
    hantsy
    hantsy
    Jul 10, 2020
    10
    另外,可能 Instanise 顺序的问题,在加载 Filter 的时候,依赖可以没有实例。
    用 @Autowired 加上 @Lazy 。最新的 Spring 2.2 可以开启全局 Lazy Loading,提高启动速度。
    wucao219101
    wucao219101
    Jul 10, 2020
    11
    @codingbody 可能是为了用 jsp
    qw564518158
    qw564518158
    OP
    Jul 15, 2020
    12
    @hfc 没异常,但是显示 null,空指针异常
    @codingbody 因为另外一些项目都是 SSM,现在分裂出了一个新的服务,才搞成微服务,为了方便运维管理吧
    qw564518158
    qw564518158
    OP
    Jul 15, 2020
    13
    @hantsy
    @Lazy
    @Autowired
    private SignaConfig signaConfig;
    测试了一下还是不行
    hantsy
    hantsy
    Jul 15, 2020
    14
    @wucao219101 jsp 太恐怖了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1515 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 15:36 · PVG 23:36 · LAX 07:36 · JFK 10:36
    ♥ Do have faith in what you're doing.