Sonal
Sonal

Reputation: 272

Is it possible to access database.properties file from outside of project structure in Hibernate?

I am trying to setup configuration of hibernate project with database.properties file outside of project. tried to set below in web.xml

<context-param>
        <param-name>propertiesLocation</param-name>
        <param-value>classpath:resources/database.properties</param-value>
    </context-param> 

and in sdnext-servlet.xml

<context:property-placeholder location="file:${#{contextParameters.propertiesLocation}" /> <br><br>

but failed to achieve required output. still above configuration forces to put property file in project structure what can be done so that it is accessible outside of project?

This below configuration is working fine for database.properties file inside project. <context:property-placeholder location="classpath:resources/database.properties"/>

what changes need to be done so that project can access database.properties file which is outside of project.

web.xml file is

<servlet>
        <servlet-name>sdnext</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/sdnext-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sdnext</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

sdnext-servlet.xml is

<context:property-placeholder location="classpath:resources/database.properties"/>
    <context:component-scan base-package="com.dineshonjava" />
    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />

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

    <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.dineshonjava.model.Employee</value>
                <value>com.dineshonjava.model.Books</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

Upvotes: 0

Views: 783

Answers (3)

M. Deinum
M. Deinum

Reputation: 125018

As stated it has nothing to do with Hibernate or another framework besides Spring. It has to do with your misunderstanding of placeholders (processing) and Spring EL.

You are trying to configure a <context:property-placeholder /> using placeholders. Now thing about that, you are using placeholders before the infrastructure for fully parsing those placeholders is actually in place.

If you really want to use a dynamic way of setting the configuration you will have to resort to using the Spring EL. You will actually have to do programming in xml kind of things.

You will need to use the environment variable to resolve the variable from known locations, like the system environment, system properties, jndi, servlet context etc. Use the getRequiredProperty(...) method to resolve the value.

<context:property-placeholder location="#{environment.getRequiredProperty('propertiesLocation')}" />

Note: This will not work as the location attribute isn't aware of EL. Use a plain PropertySourcesPlaceholderConfigurer instead.

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="location" value="#{environment.getRequiredProperty('propertiesLocation')}" />
</bean>

Now you need to define the property named propertiesLocation somewhere, this can be either (depending on the environment and possibilities of the environment).

  • System Environment Variable
  • System Property (java -D)
  • Servlet Context Parameter
  • Servlet Init Parameter
  • JNDI

In your case you wanted to use a context param

<context-param>
    <param-name>propertiesLocation</param-name>
    <param-value>classpath:resources/database.properties</param-value>
</context-param>

Now instead of classpath:resources/database.properties use something else like file:/some/path/on/your/system/database.properties. See the reference guide on resource loading for more information on supported prefixes.

However you probably are better of using a System Environment variable of JNDI entry for this as else it still is quite static and you are better of directly specifying location="file:/some/path/on/your/system/database.properties.

Upvotes: 1

Ant&#243;nio Ribeiro
Ant&#243;nio Ribeiro

Reputation: 4202

Following @user2004685 suggestion, if giving an absolute path to your properties file in location works, then you could surpass the hard-coded problem by defining a Maven property, containing the file path, that's replaced during your projects' build cycle.

Upvotes: 1

ninja.coder
ninja.coder

Reputation: 9658

I see a typo in file:${#{contextParameters.propertiesLocation} where you are not ending the property with }.

Try this:

<context:property-placeholder location="file:#{contextParameters.propertiesLocation}"/>

Upvotes: 0

Related Questions