如何将本地文件名传递给ClassPathXmlApplicationContext? [英] How to pass local filename to ClassPathXmlApplicationContext?

查看:135
本文介绍了如何将本地文件名传递给ClassPathXmlApplicationContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过本地文件名",我的意思是资源文件与类文件位于同一目录中.在下面的情况下,这是JUnitRunner.class文件. Java的 /'

By "local filename" I mean that resource file is located in the same directory as class file. In the case below this is JUnitRunner.class file. Java's getResource() file can handle this if path does not start with /'

我不知道如何执行相同的ClassPathXmlApplicationContext构造函数?

I can't figure out, how to do the same ClassPathXmlApplicationContext constructor?


package springtests;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JUnitRunner {

    private final static Logger log = LoggerFactory.getLogger(JUnitRunner.class);

    @Test
    public void test() throws URISyntaxException {

        String filename = "test01.xml";

        URL url = getClass().getResource(filename);


        File file = new File(url.toURI());
        log.info("File exists: {}", file.exists());

        try {
            new ClassPathXmlApplicationContext(filename);
        }
        catch(Exception e) {
            log.error("Can't load context", e);
        }

    }
}

输出如下


15:32:27,375 3    [main] INFO  springtests.JUnitRunner  - File exists: true
15:32:27,422 50   [main] INFO  org.springframework.context.support.ClassPathXmlApplicationContext  - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ee3aa7: startup date [Thu Nov 01 15:32:27 MSK 2012]; root of context hierarchy
15:32:27,475 103  [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader  - Loading XML bean definitions from class path resource [test01.xml]
15:32:27,477 105  [main] ERROR springtests.JUnitRunner  - Can't load context
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test01.xml]; nested exception is java.io.FileNotFoundException: class path resource [test01.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)

...

更新

XML文件与类文件位于同一文件夹中.从getResource()可以看到它.

XML file is located in the same folder as class file. It is visible from the fact that getResource() see it.

推荐答案

所以,包springtests中的xml文件和正确创建的AppContext应该是

so, your xml file in package springtests and correct creation of AppContext should be

new ClassPathXmlApplicationContext("springtests/test01.xml");

这篇关于如何将本地文件名传递给ClassPathXmlApplicationContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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