Maybe the title is a little bit obscure, but let me explain... In a servlet, to know the webapp root, yo开发者_运维百科u'll do:
String path = getServletContext.getRealPath("/");
the problem is that you have to be in a servlet. Now I'd like to pass this path to a generic bean, that is wired up in my servlet-context.xml . How to do that?
Your bean can request to be injected with the current ServletContext
, either by implementing the ServletContextAware
interface, or else using autowiring, i.e.
private @Autowired ServletContext servletContext;
Use whichever one fits your code & config best.
You might also to consider using Spring's ServletContextResource
class to perform filesystem access against the ServletContext
, e.g. using ServletContextResource.getFile()
.
精彩评论