在Spring中将数据库连接存储在资源变量中

时间:2016-04-12 12:49:57

标签: java spring hibernate spring-mvc

我正在使用Hibernate和Spring MVC从网格库中读取数据。我想将DB连接存储在系统变量中。我所做的是创建一个名为database.properties的文件,并编写从文件中读取的代码"



<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/**"></mvc:resources>
	<context:property-placeholder location="classpath:resources/database.properties"/>
	<context:component-scan base-package="com.dineshonjava"/>

	<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
	
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		id="jspViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		id="dataSource">
		<property name="driverClassName" value="${database.driver}"></property>
		<property name="url" value="${database.url}"></property>
		<property name="username" value="${database.user}"></property>
		<property name="password" value="${database.password}"></property>
	</bean>

	<bean
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
		id="sessionFactory">
		<property name="dataSource" ref="dataSource"></property>
		<property name="annotatedClasses">
			<list>
				<value>com.dineshonjava.model.Employee</value>
				<value>com.dineshonjava.model.Department</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 class="org.springframework.orm.hibernate4.HibernateTransactionManager"
		id="hibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
&#13;
&#13;
&#13;

如何使用系统变量?我在互联网上阅读但仍无法找到相关信息。

1 个答案:

答案 0 :(得分:0)

从您的问题中不清楚您要实现的目标。我想你正在寻找这样的东西:

<!-- In bean definition-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
    <property name="driverClassName"  value="#{ systemProperties['database.driver'] }"></property>
    <property name="url"  value="#{systemProperties['database.url'] }"></property>
    <property name="username"  value="#{ systemProperties['database.username'] }"></property>
    <property name="password" value= value="#{ systemProperties['database.password'] }"></property></bean>

您可以在properties中声明要部署应用程序的boxes(Unix/Windows)