简体   繁体   中英

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

I am trying to setup configuration of hibernate project with database.properties file outside of project. tried to set below in web.xml

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

and in sdnext-servlet.xml

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

but failed to achieve required output. what can be done so that it is accessible outside of project? ,以便可以在项目外部进行访问吗?

This below configuration is working fine for database.properties file inside project. <context:property-placeholder location="classpath:resources/database.properties"/>

what changes need to be done so that project can access database.properties file which is outside of project.

web.xml file is

<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 is

<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>

As stated it has nothing to do with Hibernate or another framework besides Spring. It has to do with your misunderstanding of placeholders (processing) and Spring EL .

You are trying to configure a <context:property-placeholder /> using placeholders. Now thing about that, you are using placeholders before the infrastructure for fully parsing those placeholders is actually in place.

If you really want to use a dynamic way of setting the configuration you will have to resort to using the Spring EL. You will actually have to do programming in xml kind of things.

You will need to use the environment variable to resolve the variable from known locations, like the system environment, system properties, jndi, servlet context etc. Use the getRequiredProperty(...) method to resolve the value.

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

Note: This will not work as the location attribute isn't aware of EL. Use a plain PropertySourcesPlaceholderConfigurer instead.

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

Now you need to define the property named propertiesLocation somewhere, this can be either (depending on the environment and possibilities of the environment).

  • System Environment Variable
  • System Property (java -D)
  • Servlet Context Parameter
  • Servlet Init Parameter
  • JNDI

In your case you wanted to use a context param

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

Now instead of classpath:resources/database.properties use something else like file:/some/path/on/your/system/database.properties . See the reference guide on resource loading for more information on supported prefixes.

However you probably are better of using a System Environment variable of JNDI entry for this as else it still is quite static and you are better of directly specifying location="file:/some/path/on/your/system/database.properties .

按照@ user2004685的建议,如果在location工程中给属性文件提供绝对路径,则可以通过定义包含文件路径的Maven属性(在项目的构建周期中替换该属性)来解决硬编码问题。

I see a typo in file:${#{contextParameters.propertiesLocation} where you are not ending the property with } .

Try this:

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

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