文章目录
- 在Spring Context中加载properties文件
 - 测试
 - 总结
 
在Spring Context中加载properties文件
分为三步,如下图所示:
 
 
 完整代码:
<?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">
<!--    1. 开启context命名空间-->
<!--    2. 使用context空间加载properties文件-->
    <context:property-placeholder location="jdbc.properties"/>
<!--    3. 使用属性占位符${}读取properties文件中的属性-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="driverClassName" value="${jdbc.url}" />
    </bean>
</beans>
 
测试
该测试用于验证能够读取外部文件的内容
 有如下代码:
 BookDao.java
package com.example.project1.dao;
public interface BookDao {
    void save();
}
 
BookDaoImpl.java
package com.example.project1.dao.impl;
import com.example.project1.dao.BookDao;
public class BookDaoImpl implements BookDao {
    private String name;
    @Override
    public void save() {
        System.out.println("BookDaoSave...");
        System.out.println("name:" + this.name);
    }
    public void setName(String name) {
        this.name = name;
    }
}
 
在Spring config中配置这个bean,将${jdbc.username}注入到name中:
<bean id="bookDao" class="com.example.project1.dao.impl.BookDaoImpl">
    <property name="name" value="${jdbc.username}"/>
</bean>
 
用如下代码测试一下:
package com.example.project1;
import com.example.project1.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class Project1Application {
	public static void main(String[] args) {
		// IoC容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		// DataSource dataSource = (DataSource) ctx.getBean("dataSource");
		// System.out.println(dataSource);
		BookDao bookDao = (BookDao) ctx.getBean("bookDao");
		bookDao.save();
	}
}
 
结果:
 
总结

- 不加载系统属性
 
指的是假如在配置文件中写的是username=aaa,配置bookDao如下:
<bean id="bookDao" class="com.example.project1.dao.impl.BookDaoImpl">
    <property name="name" value="${username}"/>
</bean>
 
此时运行出来的name可能不是aaa,而是系统的名字:
 
 因为系统的一些变量优先级比这里的property高,如果不想这样可以在使用context空间加载properties处配置system-properties-mode="NEVER":
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
 
- 加载多个properties文件可以用逗号分隔
 - 加载所有properties文件可以用正则
*.properties - 以上写法不够标准,标准的是
classpath:*.properties - 如果不止要从工程中加载properties文件,还要从jar包等中加载,则写
classpath*:*properties 
第4、5种根据不同的需求来写



















