File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Not able to load properties from file system using spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Not able to load properties from file system using spring" Watch "Not able to load properties from file system using spring" New topic
Author

Not able to load properties from file system using spring

Kamal Bindra
Greenhorn

Joined: May 18, 2012
Posts: 8
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>
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

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



[How To Ask Questions][Read before you PM me]
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

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

Joined: May 18, 2012
Posts: 8
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

Joined: Mar 07, 2010
Posts: 1648
    
    7

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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Not able to load properties from file system using spring
 
Similar Threads
Dependency injection problem
Error: ERROR initServletBean, Context initialization failed
spring mvc annotation error
login form with database and spring security
running before I can crawl ( or how to add Spring to an existing Hibernate standalone app)