Spring xml 配置使用外部config 文件
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=testUser1
password=12345678
driver=com.mysql.jdbc.Driver
url=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>
Spring xml 配置使用外部config 文件相关推荐
- spring核心配置文件引入外部properties文件和另外的xml配置文件
spring核心配置文件引入外部properties文件和另外的xml配置文件 为什么要引入外部文件 我们使用jdbc的时候,会创建一个jdbc.properties配置文件,如果我需要在spring ...
- IOC操作Bean管理XML方式(外部属性文件)
目录 IOC操作Bean管理XML方式(外部属性文件) 前情引入: 实验演示: 1.直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池jar包 (3)创建一个bean6.xml配置文件 ...
- Spring-Bean配置-使用外部属性文件(转)
Spring-Bean配置-使用外部属性文件 所以可以通过@value注解获取配置文件的key-value,生成一个配置文件bean.用以在代码中直接使用bean的方式. •在配置文件里配置Bean时 ...
- [Spring5]IOC容器_Bean管理XML方式_外部属性文件
IOC操作Bean管理(外部属性文件) 1.直接配置数据库信息 (1)配置德鲁伊druid连接池 (2)引入德鲁伊druid连接池依赖jar包 <!--直接配置连接池--><bean ...
- [转] Spring XML配置十二个最佳实践
Spring是一个强大的JAVA应用框架,广泛地应用于JAVA的应用程序.为Plain Old Java Objects(POJOs)提供企业级服务.Spring利用依赖注入机制来简化工作,同时提高易 ...
- spring.xml配置类属性--喜闻乐见
相信大家在开发的过程中,都会写一些配置文件或者配置类来,毕竟好的编码习惯是不能硬编码的,所以配置文件和配置类就显得很重要了.但是我用久了之后发现,配置文件和配置类确实好用,但是假如有多个配置的话,那么 ...
- 【Kubernetes 018】cfssl创建证书并结合RBAC的RoleBinding配置新用户config文件操作详解
安全性是企业生产环境中的头等大事,对于访问同一集群的不同用户或者用户组来说,将权限分级是很有必要的.和很多云厂商一样,k8s也是采用按照角色和用户绑定的方式来分配权限的,这一节我们就来实际操作下,新建 ...
- Spring xml配置式的拦截器
传统的xml配置式使用拦截器拦截请求. 一.Web.xml配置 在Web.xml 配置Spring核心控制器DispatcherServlet接收所有请求. <servlet><se ...
- Spring Boot配置MinIO(实现文件上传、下载、删除)
1 MinIO MinIO 是一个基于Apache License v2.0开源协议的对象存储服务.它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片.视频.日志文件.备份数 ...
最新文章
- 生产环境实用的小脚本
- Kali Day01 --- arpspoof命令进行断网攻击(ARP欺骗)
- 杭电1597_find the nth digit
- 计算1+2+……+N的和。
- Akka编写一个RPC框架,模拟多个Worker连接Master的情况的案例
- oracle中的cursor属性有哪些,Cursor语法及理解
- 《Android 应用测试指南》——第2章,第2.4节包浏览器
- 数据包格式_RAW与JPEG格式怎么选??
- Spring Cloud 5分钟搭建教程
- Newtonsoft.Json的使用
- Spring Batch之Job级拦截器实现(四)
- 我不生产代码,只是代码的搬运工!超级多的 —— 第三方
- INT 10H显示字符串的小例子
- linux配置中端代理
- 如何使用 Alink 进行中文情感分析?
- xson 1.0.2 发布,新增byte[] buffer,支持XCO
- 逆向_base64_rc4_----笔记
- JDBC连接执行mysql存储过程报权限错误:User does not have access to metadata required to determine stored procedure
- IVM在什么情况下可以使用
- 云之道知识付费3.1.1【最新开源】全插件,独立版