【问题标题】:How to set a property with the webapp root in Spring?如何在 Spring 中使用 webapp root 设置属性?
【发布时间】:2011-03-25 14:37:39
【问题描述】:

也许标题有点晦涩,但让我解释一下......

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

问题是你必须在一个 servlet 中。现在我想将此路径传递给一个通用 bean,它连接在我的 servlet-context.xml 中。该怎么做?

【问题讨论】:

    标签: spring servlets configuration


    【解决方案1】:

    您的 bean 可以请求注入当前的 ServletContext,方法是实现 ServletContextAware 接口,或者使用自动装配,即

    private @Autowired ServletContext servletContext;
    

    使用最适合您的代码和配置的那个。

    您也可以考虑使用 Spring 的 ServletContextResource 类对 ServletContext 执行文件系统访问,例如使用ServletContextResource.getFile()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-22
      • 2018-01-20
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多