Spring框架 (6) —— 获得properties文件的值、模拟注册功能、小结

获得properties文件的值

Spring配置文件支持通过xxx.properties文件的Key获得对应的值。实现该功能是通过
通过${Key}来获得Properties文件对应Key的值

使用Spring读取配置文件必须导入新的命名空间 context
导入命名空间方法
将命名空间和约束重新拷贝一份,将对于的全部替换成 context,然后关联context本地schema约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
    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
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    ">

xmlns:context=“http://www.springframework.org/schema/context”
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"

使用Spring创建阿里巴巴 Druid连接池,读取配置文件

拷贝Mysql驱动包和druid连接池jar包到项目中

在这里插入图片描述

创建 db.properites

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbcdemo
jdbc.username=root
jdbc.password=root
jdbc.maxActive=10

applicationContext.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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- 读取db.properties配置文件 
	
		<context:property-placeholder location="classpath:db.properties"/>
			location: 配置文件地址 。必须加上前缀   classpath:
			Spring框架只要读取资源配置文件全部需要加上  classpath: (规则)
	-->
	
	<context:property-placeholder location="classpath:db.properties"/>
	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
		init-method="init" destroy-method="close"
	>
		<!-- 
			使用SpringEL表达式可以获取  xxx.properties配置文件key对应的值
			语法  :${key} 读取配置文件的值
				 #{100*200*1024} Spring可以使用算数表达式,运行阶段自动计算值
			
			注意:
				db.properties配置不能使用 username 作为key,
					Spring默认读取当前操作系统登录账号
				解决方案:统一价一个前缀即可
		-->
		<property name="driverClassName" value="${jdbc.driverClassName}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值