• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Not able to load properties from file system using spring

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am facing an issue regarding not able to load properties from file system defined on my local.
I am able to successfully connect to MySQL db using hibernate and spring but the problem is that i have to make db properties configurable means getting db properties from application.properties file on my file system rather than the one in classes folder in class path. i hope i am able to provide enough info wt my problem is because i have just started to use this forum so don't exactly know how to
provide info..below is the context.XML file..please help me to fix the issue...

Thanks in advance
Kamal

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:component-scan base-package="com.nstudio.itoddler.ws.action" />
<mvc:annotation-driven />

<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<bean id="mailService"
class="com.nstudio.itoddler.service.impl.MailBusinessService">
</bean>
<bean id="userService"
class="com.nstudio.itoddler.service.impl.UserBusinessService">
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="classpath:application.properties" p:defaultEncoding="UTF-8" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<context:property-placeholder location="classpath:application.properties"/>
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.nstudio.itoddler.common.UserPojo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>

<bean id="hibernatetransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>
</beans>
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags for readability.


If the properties file is not on your classpath you should be getting a FileNotFoundException. If you are getting exceptions always be sure to post them with your question along with the full stack trace.

To access a file not on the classpath try this



of course it should be the path to where your properties are located.

instead of this

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to set the 'resourceLoader' property for your ReloadableResourceBundleMessageSource? I think you should use a org.springframework.core.io.FileSystemResourceLoader and then change the 'basename' property to use the file protocol.

In summary:

replacing path/to with the absolute path of the properties file.

 
Kamal Bindra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill for your quick reply.

I am not getting any exceptions. the only thing is if i delete driver from application.properties file on my file system even then it is able to get the required value for db driver as it is not using file system's application.properties file. so what i want is if i need to change anything in future it can be done through application.properties file in local file system rather has to be through application.properties file in application. and as per your suggestion i cant use absolute path as i want to use relative path.

Thanks James for your quick reply.

I haven't tried it what you have suggested but the path that i need to provide cannot be absolute path as what i want is if i need to change anything in future it can be done through application.properties file in local file system rather has to be through application.properties file in application.


Thanks,
Kamal
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kamal Bindra wrote:Thanks Bill for your quick reply.

I am not getting any exceptions. the only thing is if i delete driver from application.properties file on my file system even then it is able to get the required value for db driver as it is not using file system's application.properties file.l



Sorry but that does not make sense. If it is not getting it from your properties file where would it come from? Do you have other PropertyPlaceholderConfigurers or properties files you are not telling us about? Perhaps a duplicate application.properties on the classpath somewhere? The value has to be loaded from somewhere.
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While this might not be what is happening. But by default PropertyPlaceholderConfigurer doesn't just stop at the .properties file. It will then check System.getProperty() then the operating environment variables. Then after that if it can't find it it will throw an exception.

Mark
 
Fire me boy! Cool, soothing, shameless self promotion:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!