Properties类
java.util.Properties包中xxx.Properties
类主要用于集中的持久存储Java
的配置文件内容,可以读取后缀是.properties
和.cfg
的配置文件。
Properties 文件内容的格式是:键=值 形式,Key值不能够重复。
它提供了几个核心的方法:
-
getProperty ( String key)
: 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。 -
load ( InputStream inStream)
: 从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。 -
setProperty ( String key, String value)
: 调用 Hashtable 的方法 put 。调用基类的put方法来设置 键 - 值对。 -
store ( OutputStream out, String comments)
: 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。 -
clear ()
: 清除所有装载的 键 - 值对。该方法在基类中提供。
写入Properties
Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
/**
* @desc: 写入Mysql数据库了连接信息到jdbc.properties中
* @author: cao_wencao
* @date: 2020-12-29 13:41
*/
public class PropertiesStoreTest {
public static void main(String[] args) {
Properties properties = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream("src/main/resources/jdbc.properties");
properties.setProperty("jdbc.driver", "com.mysql.jdbc.Driver");
properties.setProperty("jdbc.url","jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8" );
properties.setProperty("jdbc.username", "root");
properties.setProperty("jdbc.password", "123456");
// 保存键值对到文件中
properties.store(output, "Thinkingcao modify");
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
读Properties
读文件方式获取
/**
* File.separator 相当于"/"
* @throws IOException
*/
@Test
public void test2() throws IOException {
String rootPath = this.getInstance().getConfigRoot()+File.separator+"config/application.properties";
InputStream inputStream = new FileInputStream(rootPath);
Properties properties = new Properties();
properties.load(inputStream);
String property = properties.getProperty("jdbc.username");
System.out.println("property = " + property);
}
从当前的类加载器获取
/**
*三种获取方式:
* 1.InputStream inputStream = this.getClass().getResourceAsStream(name)
* 2.InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
* 3. InputStream inputStream = ClassLoader.getSystemResourceAsStream(name)
*
* @throws IOException
*/
@Test
public void test1() throws IOException {
//从当前的类加载器的getResourcesAsStream来获取
InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties");
// 2.从当前的类加载器的getResourcesAsStream来获取
// InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/application.properties");
Properties properties = new Properties();
properties.load(inputStream);
String property = properties.getProperty("jdbc.url");
System.out.println("property = " + property);
}
使用Spring-core包中的ClassPathResource读取
/**
*
* Resource resource = new ClassPathResource(path)
*
* @throws IOException
*/
@Test
public void test4() throws IOException {
Resource resource = new ClassPathResource("config/application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
String property = properties.getProperty("jdbc.url");
System.out.println("property = " + property);
}
Properties配合Spring框架使用
加载.properties
-
加载.properties方式一
<!-- 1.加载 jdbc.properties 配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>
-
加载.properties方式二
<!-- 4.引入外部配置文件 由于后期可能会引入多个配置文件 所以采用list的形式 -->
<bean id="propertyPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/config/jdbc.properties</value>
<value>classpath:/config/application.properties</value>
</list>
</property>
</bean>