我正在尝试使用项目外部的database.properties 文件设置hibernate 项目的配置。尝试在 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
的属性在某个地方,这可以是其中之一(取决于环境和环境的可能性)。
- 系统环境变量
- 系统属性 (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
的内容。请参阅 resource loading 上的引用指南有关支持的前缀的更多信息。
但是,您可能最好使用 JNDI 条目的系统环境变量,否则它仍然相当静态,您最好直接指定 location="file:/some/path/on/your/system/database.properties
.
关于java - 是否可以从 Hibernate 中的项目结构外部访问database.properties 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35173187/