Spring中的Bean配置 (12)——使用外部属性文件

在这里插入图片描述
Spring中的Bean配置 (1)——内容提要
Spring中的Bean配置 (2)—— IOC和DI
Spring中的Bean配置 (3)——在Spring的IOC容器里配置Bean(通过全类名(反射))——基于XML文件的方式
Spring中的Bean配置 (4)——依赖注入方式
Spring中的Bean配置 (5)——字面值
Spring中的Bean配置 (6)——引用其他Bean
Spring中的Bean配置 (7)——注入参数详解:null值和级联属性
Spring中的Bean配置 (8)—— 集合属性
Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配
Spring中的Bean配置 (10)—— 继承 Bean 配置和依赖 Bean 配置
Spring中的Bean配置 (11)——Bean的作用域
Spring中的Bean配置 (12)——使用外部属性文件
Spring中的Bean配置 (13)—— Spring表达式语言:SpEl
Spring中的Bean配置 (14)——IOC容器中Bean的生命周期
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
Spring中的Bean配置 (16)——在Spring的IOC容器里配置Bean(通过FactoryBean)——基于XML文件的方式

Spring中的Bean配置 (17)——在Spring的IOC容器里配置Bean——基于注解的方式来配置Bean

  • 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离
  • Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
  • Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用。
注册 PropertyPlaceholderConfigurer
  • Spring 2.5 之后:***可通过 **< context:propert-placeholder > 元素简化:

– 中添加 context Schema 定义

–在配置文件中加入如下配置:

在这里插入图片描述

在演示此代码之前必须要而外加入两个包

如下

在这里插入图片描述

dp.properties

jdbc.user=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Drive
jdbc.maxPoolSize=10

application.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"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd">

<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:dp.properties"/>

<!-- 配置C3P0数据源 -->
<bean id="datasources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>

</bean>

Main.java


import javax.sql.DataSource;

public class Main {

	public static void main(String[] args) throws SQLException {

	ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
		
		  DataSource dataSource=(DataSource)applicationContext.getBean("datasource");
		System.out.println(dataSource.getConnection());	   
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值