说明:前三种是不被Spring管理的读取配置文件内容,后面是被Spring管理的读取配置文件内容
第一种:读取application.properties中的内容
第一种方式需要添加依赖
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
</dependency>
public static void test01(){
Config load = ConfigFactory.load("application.properties");
String string = load.getString("ruoyi.name");
System.out.println(load);
System.out.println(string);
}
第二种:读取application.yml中的内容
public static void test02() throws IOException {
Resource resource = new ClassPathResource("application.yml");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
String property = props.getProperty("addressEnabled");
System.out.println(property);
}
第三种:以BufferedInputStream流的方式读取配置文件内容
public static void test03() throws Exception{
//2、通过InputStream进行读取文件
//可以读取任意路径的文件
Properties properties = new Properties();
// 使用InPutStream流读取properties文件
BufferedReader bufferedReader =
new BufferedReader(new FileReader("E:/config.properties"));
properties.load(bufferedReader);
properties.getProperty("ruoyi.name");
}
本文介绍了三种非Spring管理的配置文件读取方法:1) 使用Typesafe Config库读取`application.properties`;2) 通过`PropertiesLoaderUtils`加载`application.yml`内容;3) 以BufferedInputStream流读取`config.properties`。此外,还提及了Spring管理的配置文件读取方式。
1万+

被折叠的 条评论
为什么被折叠?



