Jump to content

Just Spring - Chapter 1 Using property files

Vicki Lopez's Photo
Posted Jul 03 2012 04:35 AM
981 Views

I am recreating the code for the Just Spring examples. I am trying to run Chapter 1 example 16(not sure if it is 16 or 17 - they are a little mixed together), using the property file.
My project name is JustSpring and the property file is located in
JustSpring/src/example16-reader-beans.properties
#property=value
file-name="src/resources/trades-data.txt"
ftp-host = "madhusudham.com"
ftp-port="10009"
component-name="TradeFileReader"

When I use the XML file JustSpring\src\resources\ex16-reader-beans.xml I get an exception which I will show at the end of this post.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springfra...g-beans-3.0.xsd
http://www.springfra.../schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder
location="classpath:ex16-reader-beans.properties"/>
<bean name="reader" class="ch1.ex17.FileReader">
<constructor-arg value="${file-name}"/>
<property name="componentName" value="${component-name}"/>
</bean>

<!-- <bean name="reader"
class="ch1.ex17.FtpReader">
<constructor-arg value="${ftp-host}" />
<constructor-arg value="${ftp-port}" />
</bean>
-->
</beans>

If I substitute the actual value for the constructor-arg value:
<constructor-arg value="src/resources/trades-data.txt"/>
It runs fine. I have noticed that in the code that is provided for the example, the actual value is used as well.

What must I do differently to get the program to work properly? Thank you.
Vicki

Here is my error message
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apach...q.html#noconfig for more info.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reader' defined in class path resource [ex16-reader-beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [ch1.ex17.FileReader]: Constructor threw exception; nested exception is java.io.FileNotFoundException: "src\resources\trades-data.txt" (The filename, directory name, or volume label syntax is incorrect)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:286)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:574)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at ch1.ex16.DataReaderClient.<init>(DataReaderClient.java:20)
at ch1.ex16.DataReaderClient.main(DataReaderClient.java:30)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [ch1.ex17.FileReader]: Constructor threw exception; nested exception is java.io.FileNotFoundException: "src\resources\trades-data.txt" (The filename, directory name, or volume label syntax is incorrect)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:105)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278)
... 15 more
Caused by: java.io.FileNotFoundException: "src\resources\trades-data.txt" (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at ch1.ex17.FileReader.<init>(FileReader.java:26)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
... 17 more

Tags:
0 Subscribe


1 Reply

0
  Vicki Lopez's Photo
Posted Jul 03 2012 04:41 AM

Wow!! I just figured it out myself. I took the " " out of my properties file and it worked great!!