使用ClassPathXmlApplicationContext的applicationContext.xml的正确路径 [英] Right path to applicationContext.xml using ClassPathXmlApplicationContext

查看:1135
本文介绍了使用ClassPathXmlApplicationContext的applicationContext.xml的正确路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have working web-application with applicationContext.xml in WEB-INF/config/applicationContext.xml. Now i need to implement some testing tool as standalone application, that can use the same applicationContext.xml, but have problem with path to config for ClassPathXmlApplicationContext class.

我知道当我将applicationContext.xml复制到默认包(其中.java驻留)时,我可以使用ClassPathXmlApplicationContext(applicationContext.xml),但这是必要的吗?

I know that when i copy applicationContext.xml to default package (where .java resides) i can use ClassPathXmlApplicationContext("applicationContext.xml"), but is this necessary ?

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);
    }

}


推荐答案

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

In a local test scenario, you are not inside a Jar, so you don't need Classpath-based access. Use FileSystemXmlApplicationContext instead.

可能是这样的:

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

(路径与执行目录相对)

(paths are relative from the execution directory)

这篇关于使用ClassPathXmlApplicationContext的applicationContext.xml的正确路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆