Akshay Siddhpura
Akshay Siddhpura

Reputation: 35

How can I use environment variable in hibernate config file?

I am currently using database parameters in hibernate(Version 4.0.1) file through properties file.

I want to use some database parameters from environment variable. How can I fetch values from java files and set in to xml files before that loads in context.

<bean id="propertyConfigurer"
    class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor" />
        <property name="locations">
            <list>
                <value>classpath:/test/demo/prop/DataParam.properties</value>
            </list>
        </property>
    </bean>

<bean id="data" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass">
            <value>${driverClass}</value>
        </property>
        <property name="jdbcUrl">
            <value>${dbconnecturl}</value>
        </property> 
        .
        .
        .
</beans>

I got some idea to make an object of Configuration class but I don't know where to write that code and how it would be implemented.

Upvotes: 1

Views: 3354

Answers (2)

Umais Gillani
Umais Gillani

Reputation: 608

You can use as <property name="username" value="#{systemProperties['dbUsername']}"/> for instance.

The variable systemProperties is predefined, you can see Xml Based Configuration for more details.

Upvotes: 1

Vasu
Vasu

Reputation: 22402

You need to use spring expression language to configure the properties from OS environemnt variables as shown below:

<bean id="propertyConfigurer"
        class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
            <constructor-arg ref="configurationEncryptor" />
            <property name="locations">
                <list>
                    <value>classpath:/test/demo/prop/DataParam.properties</value>
                </list>
            </property>
        </bean>

    <bean id="data" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass">
                <value>#{ systemProperties['driverClass']}</value>
            </property>
            <property name="jdbcUrl">
                <value>#{ systemProperties['dbconnecturl']}</value>
            </property> 
            .
            .
            .
    </beans>

Upvotes: 1

Related Questions