如何在数据库属性文件中获取用户名和密码的值,而不是hibernate.cfg.xml

时间:2015-09-22 11:58:44

标签: java spring hibernate postgresql spring-mvc

您好我是spring和hibernate的新手我想使用来自我的database.properties文件的用户名和密码的值,而不是我的hibernate.cfg.xml。我不知道也许我在某处做错了。在我的hibernate.cfg.xml文件中,我已经注释掉了用户名和密码,因为它在我提供它时有效。我还将我的database.properties文件放在与项目不同的位置

database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/mzanzi-fm
database.user=postgres
database.password=1234
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

这是我的hibernate.cfg.xml文件

<hibernate-configuration>
 <session-factory>
      <property  name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
      <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
      <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mzanzi-fm</property>
      <property name="show_sql">true</property>
      <property name= "hbm2ddl.auto">update</property>

      <mapping class="com.mzanzi.admin.model.User"/>
      <mapping class="com.mzanzi.admin.model.Genres"/>
      <mapping class="com.mzanzi.admin.model.Album"/>
      <mapping class="com.mzanzi.admin.model.Artists"/>
      <mapping class="com.mzanzi.admin.model.Songs"/>
      <mapping class="com.mzanzi.admin.model.Usergroups"/>


   </session-factory>
 </hibernate-configuration>

这是我的上下文文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
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/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/mvc 
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<context:property-placeholder location="file:C:\Users\Tumi Koma\Documents\resources\database.properties" />
<context:component-scan base-package="com.mzanzi.admin" />

<mvc:annotation-driven />
<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/pages/</value> 
    </property>
    <property name="suffix">
       <value>.jsp</value>
    </property>
</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.mzanzi.admin.model.User</value>
            <value>com.mzanzi.admin.model.Genres</value>
            <value>com.mzanzi.admin.model.Album</value>
            <value>com.mzanzi.admin.model.Artists</value>
            <value>com.mzanzi.admin.model.Songs</value>
            <value>com.mzanzi.admin.model.Usergroups</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="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- max upload size in bytes -->
    <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

    <!-- max size of file in memory (in bytes) -->
    <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

</bean>

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

非常感谢任何帮助,谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用属性占位符来实现此目的。以下是您的上下文文件的外观

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${app.jdbc.driverClassName}"
    p:url="${app.jdbc.url}" p:username="${app.jdbc.username}" p:password="${app.jdbc.password}"
    p:validationQuery="SELECT 1" />

这是你的datasource.properties文件

app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost:3306/testdb
app.jdbc.username=root
app.jdbc.password=password

要实现此目的,请在配置文件中添加(假设您在类路径中有文件)

        <context:property-placeholder
    location="classpath:datasource.properties,classpath:mailsender.properties,classpath:repository.properties" />

datasource.properties文件对应于您的database.properties文件。

this帖子可能对您有帮助。

答案 1 :(得分:0)

您的database.properties文件应位于类路径中,例如在java源的根文件夹中 - 因此属性文件与您的类在应用程序类加载器的根上下文中的位置相同。

如果您正在使用maven,则应将其放入src/main/resources

然后在您的上下文文件中尝试:

<context:property-placeholder location="classpath:database.properties" />