簡體   English   中英

是否可以從Hibernate中的項目結構外部訪問database.properties文件?

[英]Is it possible to access database.properties file from outside of project structure in Hibernate?

我正在嘗試使用項目外部的database.properties文件設置休眠項目的配置。 試圖在下面的web.xml中設置

<context-param>
        <param-name>propertiesLocation</param-name>
        <param-value>classpath:resources/database.properties</param-value>
    </context-param> 

並在sdnext-servlet.xml中

<context:property-placeholder location="file:${#{contextParameters.propertiesLocation}" /> <br><br>

但未能實現所需的輸出。 仍然在配置之上會強制將屬性文件放入項目結構中 ,以便可以在項目外部進行訪問嗎?

對於項目內的database.properties文件,此以下配置正常工作。 <context:property-placeholder location="classpath:resources/database.properties"/>

需要進行哪些更改,以便項目可以訪問項目外部的database.properties文件。

web.xml文件是

<servlet>
        <servlet-name>sdnext</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/sdnext-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sdnext</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

sdnext-servlet.xml是

<context:property-placeholder location="classpath:resources/database.properties"/>
    <context:component-scan base-package="com.dineshonjava" />
    <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/views/" />
        <property name="suffix" value=".jsp" />
    </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.dineshonjava.model.Employee</value>
                <value>com.dineshonjava.model.Books</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="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

如前所述,它與Hibernate或Spring之外的其他框架無關。 這與您對占位符(處理)和Spring EL的誤解有關。

您正在嘗試使用占位符配置<context:property-placeholder /> 現在,在基礎結構完全可用之前,您正在使用占位符。

如果您真的想使用動態方式來設置配置,則必須訴諸使用Spring EL。 實際上,您將不得不使用xml之類的程序進行編程

您將需要使用environment變量來從已知位置解析變量,例如系統環境,系統屬性,jndi,servlet上下文等。請使用getRequiredProperty(...)方法來解析值。

<context:property-placeholder location="#{environment.getRequiredProperty('propertiesLocation')}" />

注意:由於location屬性不了解EL,因此無法使用。 請改用普通的PropertySourcesPlaceholderConfigurer

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="location" value="#{environment.getRequiredProperty('propertiesLocation')}" />
</bean>

現在,您需要在某個位置定義名為propertiesLocation ,該propertiesLocation可以是(取決於環境和環境的可能性)。

  • 系統環境變量
  • 系統屬性(java -D)
  • Servlet上下文參數
  • Servlet初始化參數
  • 日本國家發展研究院

在您的情況下,您想使用上下文參數

<context-param>
    <param-name>propertiesLocation</param-name>
    <param-value>classpath:resources/database.properties</param-value>
</context-param>

現在,代替classpath:resources/database.properties使用其他file:/some/path/on/your/system/database.properties例如file:/some/path/on/your/system/database.properties 有關支持的前綴的更多信息,請參見有關資源加載的參考指南。

但是,您可能最好為此使用JNDI條目的“系統環境”變量,否則它仍然是相當靜態的,並且最好直接指定location="file:/some/path/on/your/system/database.properties

按照@ user2004685的建議,如果在location工程中給屬性文件提供絕對路徑,則可以通過定義包含文件路徑的Maven屬性(在項目的構建周期中替換該屬性)來解決硬編碼問題。

我在file:${#{contextParameters.propertiesLocation}中看到一個錯字,其中該屬性未以}結尾。

嘗試這個:

<context:property-placeholder location="file:#{contextParameters.propertiesLocation}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM