在Hibernate中可以从项目结构的外部访问database.properties文件吗? [英] Is it possible to access database.properties file from outside of project structure in Hibernate?

查看:121
本文介绍了在Hibernate中可以从项目结构的外部访问database.properties文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在项目之外设置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>
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 =prefixvalue =/ WEB-INF / views //>
< property name =suffixvalue =。jsp/>
< / bean>

< bean id =dataSource
class =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =$ {database.driver}/>
< property name =urlvalue =$ {database.url}/>
< property name =usernamevalue =$ {database.user}/>
< property name =passwordvalue =$ {database.password}/>
< / bean>

< bean id =sessionFactory
class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =annotatedClasses>
< list>
< value> com.dineshonjava.model.Employee< / value>
< value> com.dineshonjava.model.Books< / value>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< 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>
< /道具>
< / property>
< / bean>

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


解决方案

如上所述,它与Hibernate或其他Spring之外的框架。它与你对占位符的误解(处理)和春季EL

您正尝试使用占位符配置< context:property-placeholder /> 。现在关于这一点,您在基础结构之前使用占位符来完全解析这些占位符实际已经到位。

如果你真的想用动态的方式来设置配置,你将不得不使用Spring EL。您实际上必须在xml 中进行编程。

您需要使用环境变量来解析来自已知位置的变量,如系统环境,系统属性,jndi,servlet上下文等。使用 getRequiredProperty(...)方法解决这个问题。

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

注意:这不适用于位置属性不知道EL。改用普通的 PropertySourcesPlaceholderConfigurer

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

现在您需要定义名为 propertiesLocation 某处,这可以是(取决于环境和环境的可能性)。


  • 系统环境变量

  • 系统属性(java -D)
  • Servlet上下文参数

  • Servlet初始化参数

  • JNDI



在你的情况下,你想使用上下文参数

 <的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 。请参阅资源加载有关支持的前缀的更多信息。



然而,您可能更愿意使用JNDI条目的系统环境变量,因为它仍然非常静态,而且您更好直接指定 location =file:/some/path/on/your/system/database.properties


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. still above configuration forces to put property file in project structure 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.

这篇关于在Hibernate中可以从项目结构的外部访问database.properties文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆