2015-06-21 66 views
1

HI我有一个简单的Java Spring IO项目,其中类应该从csv文件中读取,并且对于每个记录读取,参数都存储在一个帐户对象列表中。 我正在使用强制IDE Luna,读取文件的类CsvAccountDao与xml文件的第一个bean中定义的csv文件位于同一个包中。 xml文件也位于相同的包中。这里是Bean文件:Spring中的类路径探针:未找到文件异常

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="https://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 

    <bean id="accountDao" 
    class="com.springinpractice.ch01.model.dao.csv.CsvAccountDao"> 
    <property name="csvResource" value="accounts.csv"></property> 
    </bean> 
    <bean id="accountService" 
      class="com.springinpractice.ch01.service.AccountService"> 
      <property name="accountDao" ref="accountDao"</property>  
     </bean> 
</beans> 

,这里是类文件CscAccountDao:

package com.springinpractice.ch01.model.dao.csv; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.math.BigDecimal; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import org.springframework.core.io.Resource; 
import com.springinpractice.ch01.model.Account; 
import com.springinpractice.ch01.model.dao.AccountDao; 

public class CsvAccountDao implements AccountDao { 
    private Resource csvResource; 

    public CsvAccountDao() { 
     // TODO Auto-generated constructor stub 
    } 
    public void setCsvResource(Resource csvFile){ 
     this.csvResource = csvFile; 
    } 
    @Override 
    public List<Account> findAll() throws Exception { 
     List<Account> results = new ArrayList<Account>(); 

     DateFormat fmt = new SimpleDateFormat("MMddyyyy"); 
     BufferedReader br = new BufferedReader(
       new FileReader(csvResource.getFile())); 
     String line; 
     while((line = br.readLine()) != null){ 
      String[] fields = line.split(","); 

      String accountNo = fields[0]; 
      BigDecimal balance = new BigDecimal(fields[1]); 
      Date lastPaidOn = fmt.parse(fields[2]); 
      Account account = 
         new Account(accountNo, balance, lastPaidOn); 
      results.add(account); 
     } 
     br.close(); 
     return results; 
    } 

} 

记下方法setCsvResource CSV文件分配给资源对象是异常的问题开始的地方。我得到的堆栈跟踪,说一个异常错误:

Jun 20, 2015 7:59:42 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]177e4b0: defining beans [accountDao,accountService]; root of factory hierarchy 
Exception in thread "main" java.io.FileNotFoundException: class path resource [accounts.csv] cannot be resolved to URL because it does not exist 

Q)是否有一个问题,我将不得不更新,其中第一豆加入了全路径使用accounts.csv XML文件?既然类,xml和csv文件都在同一个包中,我认为我不需要更具体。谢谢

谢谢

+0

检查此http:// stackoverflow。com/questions/7484594/spring-3-0-inject-files-as-resources – Satya

+0

谢谢Satya我将该软件包添加到我的应用程序COntext XML文件中的account.csv文件中,并且它工作正常。 – user1548875

回答

2

一些背景资源加载。

在Java中,要加载类路径资源,请在具有资源名称的类加载器上调用getResourcegetResourceAsStream。资源名称为:

资源的名称是用'/'分隔的路径名,用于标识资源。

所以从您使用包获取资源:

classloader.getResource("org/com/resource.jpg") 

一个Java类,也有方便的方法来从它的类加载器加载资源。如果使用ClassA.class.getResource("resource.jpg"),它将在ClassA的'/'加入包之前加上“resource.jpg”并委托给类加载器。如果你的资源字符串以'/'开始,它将被解释为绝对路径,那么将删除开头的'/',然后委托给类加载器。

那么,我们会弹出提供accounts.csv作为要转换为资源的值。 Spring使用DefaultResourceLoader来查找您的资源。下面是使用它来找到你的资源的逻辑:

@Override 
public Resource getResource(String location) { 
    Assert.notNull(location, "Location must not be null"); 
    if (location.startsWith("/")) { 
     return getResourceByPath(location); 
    } 
    else if (location.startsWith(CLASSPATH_URL_PREFIX)) { 
     return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); 
    } 
    else { 
     try { 
      // Try to parse the location as a URL... 
      URL url = new URL(location); 
      return new UrlResource(url); 
     } 
     catch (MalformedURLException ex) { 
      // No URL -> resolve as resource path. 
      return getResourceByPath(location); 
     } 
    } 
} 

你的情况,它属于所有的方式通过对catch(MalformedURLException ex),然后尝试看看它的类路径的资源。当最终要求ClasspathResource输入URL或输入流时,它会将"account.csv"传递给类加载器。类加载器正在类路径的根目录中查找它。

所以在你的spring xml中,你需要告诉它它是什么包。它不会相对于xml。因此,将其更改为“/package/account.csv”或“classpath:/package/account.csv”

仅供参考:我正在查看Spring 4.0.9.RELEASE和oracle jdk8。

+0

感谢Roby和Satya我将该包添加到我的应用程序COntext XML文件中的account.csv文件中,并且它工作正常。 – user1548875