2011-03-25 24 views
0

也許標題是有點不起眼,但讓我解釋...在一個servlet,要知道Web應用程序根目錄,你會做:如何在Spring中使用webapp root設置屬性?

String path = getServletContext.getRealPath("/"); 

的問題是,你必須要在servlet的。現在我想把這個路徑傳遞給一個泛型bean,這個泛型bean在我的servlet-context.xml中被連接起來。怎麼做?

回答

1

你的bean可以請求與當前ServletContext注入,或者通過實現ServletContextAware接口,否則使用自動裝配,即

private @Autowired ServletContext servletContext; 

使用哪一個適合你的代碼&配置最好的。

您也可以考慮使用Spring的ServletContextResource類對ServletContext執行文件系統訪問,例如,使用ServletContextResource.getFile()

相關問題