0

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

4

1 回答 1

0

您可以使用返回 DataSource 对象的方法创建自定义工厂 Bean。在 Factory Bean 方法中,您将完全控制 DS 对象的创建方式,因此,例如,您可以从 Spring 配置中获取除密码之外的所有属性,但密码将以不同的方式找到。

只要您可以保证 monitor.properties 文件位于类路径上,还有更简单的方法。在这种情况下,您不需要设置确切的属性文件位置,而是使用

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

如果您的 monitor.properties 中有密码属性,则可以将其引用为 ${password}

(查看完整示例:http ://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/ )

于 2013-04-21T19:29:39.290 回答