Spring xml 配置使用外部config 文件
来源:互联网 发布:专业彩妆品牌知乎 编辑:程序博客网 时间:2024/04/27 01:00
Spring xml 配置使用外部config 文件
当使用Spring framework后, 我们一般会把db connection的信息写在spring的bean config xml里面。
例如:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="testUser1"></property> <property name="password" value="12345678"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://homeServer:3306"></property> </bean> </beans>
但是在项目中, 我们的程序一般在不同的环境中运行,如果每次都去修改xml就会出现如下的问题。
1.容易出错
2.需要重新打包(xml在包里的话)
3.程序猿会见到密码等敏感信息。
所以我们一般会把项目deployment的信息例如db Conection写在 外部的1个 config file中。
例子
首先在系统中随便1个位置建立1个config 文件:
/home/gateman/Studies/java_start/configfiles/mysql.config
user=testUser1password=12345678driver=com.mysql.jdbc.Driverurl=jdbc:mysql://homeServer:3306
在spring xml中引入 PropertyPlaceholderConfigurer 这个bean
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="file:/home/gateman/Studies/java_start/configfiles/mysql.config"></property> </bean>
注意 上面的location value就是配置文件的位置
file:xxx 代表绝对路径
classpath: xxx 代表类路径
最后, 在dataSoruce 这个bean就可以引用config file的值了
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="file:/home/gateman/Studies/java_start/configfiles/mysql.config"></property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="driverClass" value="${driver}"></property> <property name="jdbcUrl" value="${url}"></property> </bean> </beans>
0 0
- Spring xml 配置使用外部config 文件
- MappingDispatchAction 的使用&structs-config.xml配置&spring xml文件配置
- C# 使用app.config文件方法 读取外部配置信息
- Spring-Bean配置-使用外部属性文件
- Spring之config.xml配置
- 一个spring-config.xml文件
- spring.xml从外部文件引入数据库配置信息
- mybatis-config.xml文件配置
- mybatis-config.xml文件配置
- 配置Struts应用使用多个struts-config.xml文件
- spring cloud config server使用svn来作为外部配置中心
- Spring中配置bean时使用外部属性文件
- Spring-config.xml数据库操作Bean配置
- spring 使用外部属性文件
- spring容器使用外部文件
- Spring 使用外部属性文件
- Spring使用外部属性文件
- Spring使用外部属性文件
- TensorFlow人工智能引擎入门教程所有目录
- php数组----初探谈任何语言恒古不变的是算法和思想谈与Java中的数组对比
- Wiggle Sort
- SpringMVC上传文件例子
- Android XML文件中的属性详情
- Spring xml 配置使用外部config 文件
- iOS逆向工程读书笔记
- STL中的lower_bound() 和 upper_bound()
- 0 1 背包问题
- 打造绚丽的ViewPager
- hdoj 瞬间移动 5698(逆元)
- java返回1到54之间不包括某些数的随机数
- 状态栏
- 【Spring】【笔记】《Spring In Action》第二章 Bean装配