【发布时间】:2016-05-12 10:07:42
【问题描述】:
我正在尝试使用项目外部的 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>
【问题讨论】:
-
您是否尝试在
location中提供属性文件的绝对路径? -
@ user2004685 是的,我试过这样
所以它工作正常。但我正在尝试这样做,以便它不应该在任何一个文件中硬编码。有可能吗? -
file:${#{contextParameters.propertiesLocation}在这里你没有结束}。是不是打错字了? -
@ user2004685 是的,这只是这里的编辑错误。当我使用上面的配置得到这个: java.io.FileNotFoundException: ${ (No such file or directory)
-
占位符不起作用,您可以尝试
#{environment['propertiesLocation']}。
标签: java xml spring hibernate servlets