I have a simple Java Spring IO project where a class should read from a csv file and for each record read, the parameters are stored into a list of account objects. I'm using Force IDE Luna and the Class CsvAccountDao that reads the file is in the same package as the csv file which is defined in the first bean of the xml file. The xml file is also located under the same package. Here is the bean file:
<?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>
and here is the class file 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;
}
}
Note where method setCsvResource assigns the csv file to the resource object is where the exception problem begins. I get an exception error in the stack trace that says:
Jun 20, 2015 7:59:42 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@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) Is there a problem where I would have to updated the XML file where the first bean uses accounts.csv by adding a full path? Since the both the class,xml, and the csv files are within the same package I thought I didn't need to be more specific.