【问题标题】:Right path to applicationContext.xml using ClassPathXmlApplicationContext使用 ClassPathXmlApplicationContext 指向 applicationContext.xml 的正确路径
【发布时间】:2011-06-15 17:31:21
【问题描述】:

我在 WEB-INF/config/applicationContext.xml 中有使用 applicationContext.xml 的 web 应用程序。现在我需要实现一些测试工具作为独立应用程序,它可以使用相同的 applicationContext.xml,但是 ClassPathXmlApplicationContext 类的配置路径有问题。

我知道当我将 applicationContext.xml 复制到默认包(.java 所在的位置)时,我可以使用 ClassPathXmlApplicationContext("applicationContext.xml"),但这有必要吗?

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AdminTool {

    private Logger log = Logger.getLogger(getClass());

    /** all below doesn't work - FileNotFoundException) **/
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/config/applicationContext.xml");
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("../config/applicationContext.xml");

    public static void main(String[] args) {
        new AdminTool();
    }

    public AdminTool() {
        log.debug(ac);
    }

}

【问题讨论】:

    标签: java spring integration-testing


    【解决方案1】:

    请转到属性>Java 构建路径>您项目的源检查类路径并在类路径中的 WEB-INF 之前添加目录。绝对是工作

    【讨论】:

      【解决方案2】:
      private static final ApplicationContext ac =
      new FileSystemXmlApplicationContext(
          "/WEB-INF/config/applicationContext.xml"
      );
      

      【讨论】:

      • 请解释为什么下面的代码与上面的代码相比有问题。
      【解决方案3】:

      这就是为什么我更喜欢将所有配置文件放在 classes 文件夹中的原因。事实上,使用 Maven,您可以将所有这些文件放在 src/main/resources 中。然后,在您的测试文件中,您可以作为“classpath:applicationContext.xml”访问。

      【讨论】:

        【解决方案4】:
        private static final ApplicationContext ac =
            new FileSystemXmlApplicationContext(
                "/WEB-INF/config/applicationContext.xml"
            );
        

        【讨论】:

          【解决方案5】:

          在本地测试场景中,您不在 Jar 中,因此您不需要基于 Classpath 的访问。请改用FileSystemXmlApplicationContext

          大概是这样的:

          private static final ApplicationContext ac =
              new FileSystemXmlApplicationContext(
                  "src/WEB-INF/config/applicationContext.xml"
              );
          

          (路径是相对于执行目录的)

          【讨论】:

          • 我正在考虑使用 webapp.war 运行我的 AdminTool。是否可以使用 FileSystemXmlApplicationContext ?
          • @marioosh 不,在战争中您会遇到与罐子中相同的问题:没有文件,只有条目。战争是部署到 servlet 容器还是本地?
          • @Patrick 我想使用 .war 中的 AdminTool 类而不进行部署。 PS:我有 maven 项目: new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/config/applicationContext.xml");当我在 Eclipse 中运行 AdminTool 时工作
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-19
          • 2016-09-26
          • 1970-01-01
          • 2015-04-01
          • 1970-01-01
          相关资源
          最近更新 更多