我正在嘗試在項目之外設置hibernate項目與database.properties文件的配置。嘗試下面設置在web.xml在Hibernate中可以從項目結構的外部訪問database.properties文件嗎?
<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>
你有沒有嘗試給你的'位置'屬性文件的絕對路徑? – user2004685
@ user2004685是的我試過這樣的所以它的工作。但我正在嘗試這樣做,以便它不應該在任何文件中進行硬編碼。可能嗎? –
Sonal
'file:$ {#{contextParameters.propertiesLocation}'在這裏你並沒有結束'}'。這是一個錯字嗎? – user2004685