spring-210727-02–IOC容器–Bean管理xml方式-外部属性文件(properties资源文件)
- 导入需要的jar包和properties资源配置文件
- 配置bean
01 导入需要的jar包和properties资源配置文件
jdbc.properties
prop.driver=com.mysql.cj.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/test?&useSSL=false&serverTimezone=UTC
prop.username=root
prop.password=admin
02 配置bean
需要引入名称空间:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
externalresourcebean.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.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource01" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driver}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>