【问题标题】:How to get values of username and password in database properties file and not hibernate.cfg.xml如何在数据库属性文件而不是 hibernate.cfg.xml 中获取用户名和密码的值
【发布时间】:2015-12-19 09:33:50
【问题描述】:

您好,我是 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>

任何帮助将不胜感激,谢谢

【问题讨论】:

    标签: java spring hibernate postgresql spring-mvc


    【解决方案1】:

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

    <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 的帖子可能会对您有所帮助。

    【讨论】:

      【解决方案2】:

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

      如果你使用 maven,你应该把它放在src/main/resources

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-16
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        相关资源
        最近更新 更多