优化Spring配置文件中的数据库连接部分
来源:互联网 发布:淘宝拍摄灯光布置图解 编辑:程序博客网 时间:2024/06/06 05:40
在之前的SSH框架搭建的博客中,配置数据库连接时,直接将数据库的用户名、密码等信息写在了Spring配置文件中(如下图)。这样导致的结果是,如果数据库密码修改了,就需要修改Spring配置文件;如果有多个项目都使用了这个数据库用户名和密码,那么维护的成本将非常高。
Spring为我们提供了解决方案。我们可以新建一个配置文件db.properties,文件内容如下:
dataSource=com.mchange.v2.c3p0.ComboPooledDataSourcedriverClass=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/entry_exit_mgtuser=rootpassword=Urie_308
然后修改Spring配置文件:
(1)添加读取properties文件的支持。
<!-- 读取properties文件 --> <bean id="propertiesConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:db.properties"></property> <!-- 也可以写成另一种形式,可以读取多个properties文件 --> <!-- <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> --> </bean>
(2) 修改数据库配置部分
<!-- 配置数据源,这里配置了连接数据库的各项参数 --> <bean id="dataSource" class="${dataSource}"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!-- 数据库连接池的一些属性也配置在这里,这里先不配置 --> </bean>
阅读全文
0 0
- 优化Spring配置文件中的数据库连接部分
- Spring 配置文件部分详解
- spring读取配置文件优化
- 抽取 spring 数据库连接部分到项目中
- Spring Security教程第五部分-数据库连接登录
- Spring配置文件中如何使用外部配置文件配置数据库连接
- .Net配置文件中的数据库连接字符串使用
- SSH中的spring配置文件
- SSH中的spring配置文件
- Spring配置文件中的Scope
- Spring中的配置文件
- Spring配置文件中的import
- jdbc和spring的整合,部分配置文件
- Nginx配置文件优化中的比较
- 优化MyBatis配置文件中的配置
- 优化MyBatis配置文件中的配置
- Spring配置文件中配置数据库连接(mysql,sqlserver,oracle)
- Spring配置文件中的<beans>标签
- 自定义变量不能用怎么办,将文件中的数据直接读取到普通变量中
- 如何在React中做Ajax 请求?
- 小白学习日记1:PL/SQL连接本地Oracle数据库Part2
- [Azure]ARM虚拟机整机磁盘快照[1]——创建快照
- jboss domain配置
- 优化Spring配置文件中的数据库连接部分
- STM32启动文件详解1
- SVN : Working copy is too old (format 10, created by Subversion 1.6)
- 崩溃的Python学习过程2——ImportError: No module named PIL(windows)
- 开发环境部署
- 1185: 添加记录(结构体专题)
- yii2 批量删除
- iOS NSURLSession Https请求
- java日期工具类