springBoot用外部tomcat方式启动,读取不到xml配置

我的springBoot应用resources目录下有个urlrewrite.xml的配置文件(urlrewrite的配置)。配置了拦截器

 @Bean
    public FilterRegistrationBean urlRewriteFilter() {
        FilterRegistrationBean fitler = new FilterRegistrationBean();
        fitler.setFilter(new UrlRewriteFilter());
        fitler.setName("UrlRewriteFilter");
        fitler.addInitParameter("confPath","urlrewrite.xml");
        fitler.addInitParameter("logLevel", "info");
        fitler.addUrlPatterns("/about/index.html", "/fund/*", "/news/*", "/poster/*", "/subject/*", "/myhb/*", "/custominfomgr/*", "/verify/*", "/myinfo/*", "/user/*", "/research/*", "/trust/*");
        return fitler;
    }

采用SpringBoot自己的@SpringBootApplication方式启动没问题。使用外部的tomcat启动时,就加载不到urlrewrite.xml。
下面是UrlRewriteFilter加载配置文件的源码:

private void loadUrlRewriterLocal() {
        InputStream inputStream = context.getResourceAsStream(confPath);
        // attempt to retrieve from location other than local WEB-INF
        if ( inputStream == null ) {
            inputStream = ClassLoader.getSystemResourceAsStream(confPath);
        }
        URL confUrl = null;
        try {
            confUrl = context.getResource(confPath);
        } catch (MalformedURLException e) {
            log.debug(e);
        }
        String confUrlStr = null;
        if (confUrl != null) {
            confUrlStr = confUrl.toString();
        }
        if (inputStream == null) {
            log.error("unable to find urlrewrite conf file at " + confPath);
            // set the writer back to null
            if (urlRewriter != null) {
                log.error("unloading existing conf");
                urlRewriter = null;
            }

        } else {
            Conf conf = new Conf(context, inputStream, confPath, confUrlStr, modRewriteStyleConf);
            checkConf(conf);
        }
    }

使用Application方式启动时, ClassLoader.getSystemResourceAsStream(confPath)是会读取到的。第三方tomcat启动时inputStream就会为null。
有哪位大神碰到过类似问题?

Spring 提供了一个工具类。专门用于从classpath下读取数据:ClassPathResource

你也可以试试看,把参数值以 / 开头 。
fitler.addInitParameter("confPath","/urlrewrite.xml");

真的可以。。
贴出我的测试代码:

@Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("-----加载Filter");

        context = filterConfig.getServletContext();
        String confPath = "urlrewrite.xml";

        InputStream inputStream = context.getResourceAsStream(confPath);
        // attempt to retrieve from location other than local WEB-INF
        if ( inputStream == null ) {
            inputStream = ClassLoader.getSystemResourceAsStream(confPath);

            if(inputStream == null){
                try {
                    ClassPathResource classPathResource = new ClassPathResource(confPath);
                    inputStream = classPathResource.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        URL confUrl = null;
        try {
            confUrl = context.getResource(confPath);
        } catch (MalformedURLException e) {
            e.printStackTrace();
//            log.debug(e);
        }
        String confUrlStr = null;
        if (confUrl != null) {
            confUrlStr = confUrl.toString();
        }
        Conf conf = new Conf(context, inputStream, confPath, confUrlStr, modRewriteStyleConf);
        log.info("----");
    }

使用第三方tomcat启动时,会在ClassPathResource classPathResource = new ClassPathResource(confPath);处得到inputStream

1赞

解决了就好。看到我回复信息下面那个【解决方案】了么?点一下。:joy:

1赞