开发者

How to set a property with the webapp root in Spring?

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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜