加载properties文件

// resources/jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root

修改前Spring配置文件

<?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">

</beans>

修改后

<?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->xmlns:context beans->context
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans  // 复制该行
           http://www.springframework.org/schema/beans/spring-beans.xsd  // 复制该行
           http://www.springframework.org/schema/context  // 粘贴 beans->context
           http://www.springframework.org/schema/context/spring-context.xsd  // 粘贴 beans->context
           ">

// 1. 开启context命名空间

// 2. 使用context空间加载properties文件
    <context:property-placeholder location="jdbc.properties"/>
    // <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/> // 不加载系统属性

// 3. 使用属性占位符${}读取properties文件中的属性

    <bean id="dataSource" class="com.xxx.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>

测试一下

public class BookDaoImpl implements BookDao {
    private String name;

    public void setName(String name){
        this.name = name;
    }

    public void save(){
        System.out.println("book dao save ..." + name);
    }
}
<bean id="bookDao" class="com.xxx.dao.impl.BookDaoImpl">
    <property name="name" value="${jdbc.driver}"/>
</bean>
public class App{
    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        bookDao.save();
    }
}

加载多个配置文件

<?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->xmlns:context beans->context
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans  // 复制该行
           http://www.springframework.org/schema/beans/spring-beans.xsd  // 复制该行
           http://www.springframework.org/schema/context  // 粘贴 beans->context
           http://www.springframework.org/schema/context/spring-context.xsd  // 粘贴 beans->context
           ">

// 1. 开启context命名空间

// 2. 使用context空间加载properties文件
    // 加载多个properties文件
    <context:property-placeholder location="jdbc.properties,jdbc2.properties"/>
    // 加载所有properties文件
    <context:property-placeholder location="*.properties"/>
    // 加上类路径
    <context:property-placeholder location="classpath:*.properties"/>
    // 不仅从当前工程中读,还从依赖的jar包中读
    <context:property-placeholder location="classpath*:*.properties"/>
    // <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/> // 不加载系统属性

// 3. 使用属性占位符${}读取properties文件中的属性

    <bean id="dataSource" class="com.xxx.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>