简体   繁体   中英

How to get the location specific data for hibernate context file

How to get the location specific data such as user name and propertyfile loadings at run time for hibernate context file. I am working in GWT framwork

I have this in my applicationcontext.xml

          <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url"   value="jdbc:mysql://localhost:3306/dashboardsupervisor" />
    <property name="username" value="root" />
    <property name="password" value="1234" />
</bean>

it works fine and connect with my database , Now my problem is that this password"1234" is in some file "monitor.properties" and the location of this file is UNKNOWN, I do not know the file location , it varies from machine to machine .

Any idea how can i get the password here instead of "1234" a password that is in the file which location in unknown ..

thanks

You can create custom Factory Bean with a method that would return DataSource object. In the Factory Bean method you will have total control on the way DS object is created, so, for instance, you can take all properties, except password, from Spring configuration, but password will be found in a different way.

There is also easier approach as long as you can guranantee that monitor.properties file is on the classpath. In this case you don't need to set exact propertis file location, but use

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="monitor.properties" />
</bean>

if you have password property in your monitor.properties, you can reference it as ${password}

(see full example: http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM