Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Get file under WEB-INF without a request

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.

All solutions I can find either wants an XML file using ClassPathXmlApplicationContext or a request to get the servlet context or using the current executing class. Seems ugly to me.

How can I get a File("/WEB-INF/myDir/"). There has to be a way, no!?

like image 235
mjs Avatar asked Jun 26 '12 15:06

mjs


1 Answers

As long as your bean is declared in web application context you can obtain an instance of ServletContext (using ServletContextAware, or by autowiring).

Then you can access files in webapp directory either directly (getResourceAsStream(), getRealPath()), or using ServletContextResource.

EDIT by momo:

@Autowired ServletContext servletContext;  ... myMethod() {       File rootDir = new File( servletContext.getRealPath("/WEB-INF/myDIR/") ); } 
like image 130
axtavt Avatar answered Sep 21 '22 12:09

axtavt