以下是我的数据库配置当前的显示方式
<beans:bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan" value="com.xxx.test" />
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans:property name="generateDdl" value="true" />
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://ip-xxxx.ec2.xxx.com:3306/db" />
<beans:property name="username" value="user" />
<beans:property name="password" value="password" />
</beans:bean >
我想将数据库属性从这里取出并将它们放在tomcat的web.xml文件中,甚至是从配置文件中。我正在使用Spring JPA。我该怎么做?
答案 0 :(得分:2)
将此添加到您的上下文文件中!
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="config.properties">
</property>
</bean>
在src / main / resources中创建config.properties并将用户名密码存储为
mongodb.username=mongodbuser
mongodb.password=morebites
现在您可以将用户名和密码称为
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://ip-xxxx.ec2.xxx.com:3306/db" />
<beans:property name="username" value="${mongodb.username}" />
<beans:property name="password" value="${mongodb.password}" />