Spring 使用外部属性文件配置
1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容
移到属性文件中。可以在Bean配置文件使用${var}的变量,PropertyPlaceholderConfigurer从属性文件中加载属性。
一般 属性文件后点缀properties
案例:数据源的配置(配置链接数据库的信息)
处理spring 的必须包之外,我们还要导入两个架包
1.数据库的的驱动包
2.C3p0的架包
db-properties.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 导入属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库链接池配置 -->
<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="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>
</beans>
<!--外部属性文件--->
db.properties
user=root
password=sys123456
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///xbc
写一个测试方法Main,测试是否成功链接数据库
package com.spring.db;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Dbtest {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("db-properties.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println("执行中");
System.out.println(dataSource.getConnection());
System.out.println("结束");
}
Spring 使用外部属性文件配置的更多相关文章
- Spring 应用外部属性文件 配置 context 错误
在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...
- 使用外部属性文件配置Bean以及Bean的生命周期方法
1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...
- spring 使用外部属性文件
一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...
- Spring使用外部属性文件
一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...
- Spring_Bean的作用域---和使用外部属性文件
<!-- 使用 bean的scope属性来配置bean的作用域 singleton:默认值.容器初始时创建bean实例,在整个容器的生命周期内只创建这一个bean单例 prototype:原型的 ...
- IoC容器-Bean管理XML方式(引入外部属性文件)
IoC操作Bean管理(引入外部属性文件) 1,直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖jar包 2,通过引入外部属性文件配置数据库连接池 (1)创建外部属性文件,pro ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
随机推荐
- @Service注解的作用
@Service注解作用 1. 其getBean的默认名称是类名(头字母小写),可以@Service(“xxxx”)这样来指定, 2.其定义的bean默认是单例的,可以使用@Service(“b ...
- redis安装配置-linux
wget http://download.redis.io/releases/redis-3.2.9.tar.gz .tar.gz cd redis-/ make --启动 ./redis-serve ...
- ZOJ 3209
精确覆盖 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...
- php模拟并发
原文: http://blog.csdn.net/zhang_xinglong/article/details/16339867 ----------------------------------- ...
- 2015年趋势科技笔试A卷
题目原题来源:url=BHz9dr7Dbql5Ai0fTaUsi8QH-ieA9UAtw8kpf-Us_cGUnsz7ZIU1SfHIp33Cphcp0n6uPikWL6r8n0a0zQ0wNOMLG ...
- Odoo(OpenERP)开发实践:数据模型学习
作者:苏州-微尘 Odoo中,在Python类里定义的模型及字段信息,可在系统中直接查看.为用户开启技术特性权限后,就可以通过菜单 [设置->技术->数据结构->模型] 进入列表视图 ...
- php执行运算符
php执行运算符 简介 php 支持一个执行运算符:反引号(``).反引号(``)位于键盘Tab键左上方.php 将尝试将反引号中的内容作为外壳命令来执行,并将其输出信息返回(例如,可以赋给一个变量而 ...
- 修改python的pip下载源
推荐两个源: 豆瓣:http://pypi.douban.com/simple/ 清华:https://pypi.tuna.tsinghua.edu.cn/simple 使用方法有两种,一种为临时使用 ...
- AHOI 2009 (BZOJ1798)维护序列 seq (线段树好题?)
我是不会说这个题很坑的.. 改了一晚上... // by SiriusRen #include <cstdio> #define N 150000 #define LSON l,mid,l ...
- 从Oracle同步数据到SQLServer——大小写敏感设置
Oracle默认是大小写敏感,而SQLServer默认大小写不敏感, 尤其是涉及主键字段时,注意请提前设置SQLServer对应的数据库表为大小写敏感,不然会报主键冲突的错误. 设置表内大小写敏感 A ...