Spring MVC Get file under WEB-INF without a request

后端 未结 7 854
暗喜
暗喜 2020-12-24 07:08

I am trying to get hold of a file ( or a directory ) under /WEB-INF/.../

outside of a request. I need it in a bean loaded at server startup.

A

7条回答
  •  清酒与你
    2020-12-24 07:36

    Not completely related to your question, but... Here is some universal sulution i used to load properties from anywhere in web application like Spring does it (supporting WEB-INF/..., classpath:..., file:...). Is is based on using ServletContextResourcePatternResolver. You will need ServletContext.

    private static Properties loadPropsTheSpringWay(ServletContext ctx, String propsPath) throws IOException {
        PropertiesFactoryBean springProps = new PropertiesFactoryBean();
        ResourcePatternResolver resolver = new ServletContextResourcePatternResolver(ctx);
        springProps.setLocation(resolver.getResource(propsPath));
        springProps.afterPropertiesSet();
        return springProps.getObject();
    }
    

    I used the above method in my custom servlet context listener while conext was not yet loaded.

提交回复
热议问题