【问题标题】:How to get path in spring to access configuration file如何在spring中获取路径以访问配置文件
【发布时间】:2014-02-06 09:14:48
【问题描述】:

我编写了一个 Spring 程序,其中以下类、接口和 xml 文件位于不同的包中。我使用 Eclipse Kepler。

SelectClient.java

package com.rajeev.spring.action;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.rajeev.spring.DAOI.Select;

/**
 * @author rajeev
 *
 * 
 */
public class SelectClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String path=System.getProperty("user.dir");
        System.out.println(path+"/src/com/rajeev/spring/DAOImpl/SelectCfg.xml");
        Resource resource=new ClassPathResource(path+"/src/com/rajeev/spring/DAOImpl/SelectCfg.xml");
        XmlBeanFactory beanFactory=new XmlBeanFactory(resource);
        Object object=beanFactory.getBean("sb");
        Select select=(Select)object;
        System.out.println("emp name is:"+select.fetchName(101));
    }

}

问题是当我执行SelectClient.java 时,出现以下错误

E:\javahyd\eclipse\Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml 2014 年 1 月 17 日上午 11:41:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息:从类路径资源加载 XML bean 定义 [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml] 线程“主”org.springframework.beans.factory.BeanDefinitionStoreException 中的异常:IOException 从类路径资源解析 XML 文档 [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml];嵌套异常是 java.io.FileNotFoundException:类路径资源 [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml] 无法打开,因为它不存在 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 在 org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:78) 在 org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:66) 在 com.rajeev.spring.action.SelectClient.main(SelectClient.java:26) 引起:java.io.FileNotFoundException:类路径资源[E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml]无法打开,因为它不存在 在 org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 4 更多

上述异常,是由于路径所致。当我在 run(windows+r) 中使用相同的路径时,它正在打开的特定文件。我不想将我的配置文件放在任何包之外。

【问题讨论】:

  • @MagicMan 不工作
  • 试试这个:Resource resource=new ClassPathResource("com/rajeev/spring/DAOImpl/SelectCfg.xml");

标签: java xml eclipse spring spring-mvc


【解决方案1】:

ClassPathResource 用于引用资源在类路径中的路径。 ClassPathResource 中传递的path 参数指的是类路径中的绝对路径。所以,使用

Resource resource=new ClassPathResource("com/rajeev/spring/DAOImpl/SelectCfg.xml");

尝试使用FileSystemResource 提及文件系统中的绝对路径

【讨论】:

  • 我写了这个字符串 path=System.getProperty("user.dir"); System.out.println(path+"/src/com/rajeev/spring/DAOImpl/SelectCfg.xml");
【解决方案2】:

使用 FileSystemResource 代替 ClassPathResource 资源并给出完整路径,例如 Resource resource=new FileSystemResource("C://path/to//your//config.xml");

【讨论】:

  • 简单地做new FileSystemResource("C:/path/to/your/config.xml");,没有双斜杠就可以了,我在Windows 7上验证了它。如果你出于某种原因必须使用双斜杠,使用反斜杠也可以,我还在 Windows 7 上对其进行了验证/测试。就像这样new FileSystemResource("C:\\path\\to\\your\\config.xml");
猜你喜欢
  • 2022-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2017-08-07
  • 1970-01-01
  • 2014-09-17
相关资源
最近更新 更多