I’m using Spring MVC 3.0 and Tomcat.
I have a bean that has a property whose value should be a path rooted from the web document root. For example, I would specify its value in a Spring configuration file as following.
<bean id="myBean" class="com.stackoverflow.Bean">
<property name="path" value="/WEB-INF/foo.xml" />
</bean>
Where do I take this value to so that I can read the file? Does Spring/Spring MVC provide a transparent way to access resources within the document root?
In order to get the real path to the resource you will need to have access to
ServletContextOne way to achieve this will be to make your
com.stackoverflow.Bean implement ServletContextAwareinterface. Upon restart, the server should hand over an instance of ServletContext to this bean (you will have to include the following code)Finally, use
ctx.getRealPath(path)to get the real path to the resource.