resource.properties的内容:

com.tsinkai.ettp.name=imooc
com.tsinkai.ettp.website=www.imooc.com
com.tsinkai.ettp.language=java

1、使用java.util.Properties的load(InputStream inStream)方法。

先读取文件生成inputStream流,再用load加载。

    @Test
public void testReadProperties3() throws IOException {
Properties properties = new Properties();
// InputStream inputStream = this.getClass().getResourceAsStream("/resource.properties");
// InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("resource.properties");
Resource resource = new ClassPathResource("resource.properties");
InputStream inputStream = resource.getInputStream(); properties.load(inputStream);
System.out.println(properties.getProperty("com.tsinkai.ettp.name"));
}

2、使用org.springframework.core.io.support.PropertiesLoaderUtils;

    @Test
public void testReadProperties() throws IOException {
Properties properties = PropertiesLoaderUtils.loadAllProperties("resource.properties");
String name = properties.getProperty("com.tsinkai.ettp.name");
System.out.println(name);
}

3、使用java.util.ResourceBundle;

    @Test
public void testReadProperties2() throws IOException {
ResourceBundle resourceBundle = ResourceBundle.getBundle("resource");
String name = resourceBundle.getString("com.tsinkai.ettp.name");
System.out.println(name);
}

4、使用spring注解创建资源类

资源类:

package com.imooc.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; @Configuration
@ConfigurationProperties(prefix="com.imooc.opensource")//属性前缀
@PropertySource(value="classpath:resource.properties")//文件路径
public class Resource {
private String name;
private String website;
private String language; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}

调用:

package com.tsinkai.ettp;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import com.tsinkai.ettp.common.Resource; @RunWith(SpringRunner.class)
@SpringBootTest
public class EttpCustomApplicationTests { @Autowired
Resource customResource; @Test
public void readResource() {
Resource bean = new Resource();
BeanUtils.copyProperties(customResource, bean);
System.out.println(bean.getName());
}
}

最新文章

  1. SpringMVC 文件上传
  2. svn记录删除
  3. 分组函数 ----group by 使用总结
  4. 页面加载后resize页面布局
  5. 基础总结篇之三:Activity的task相关
  6. jquery异步上传文件,支持IE8
  7. 常见css的兼容问题
  8. 【pyhton】import math与import cmath
  9. Udacity调试课笔记之断言异常
  10. 达到J2EE在后台action控制接待javascript弹出的对话框
  11. SVN命令汇总
  12. Samba匿名用戶仅仅唯读访问
  13. wx小程序初体验
  14. Select的逻辑处理顺序(Transact-SQL)
  15. 小命令tac、cat、rev的用法
  16. 关于springmvc配置validator的注意事项
  17. python变量命名规则
  18. JavaScript实现接口的三种经典方式
  19. SpringBoot数据库集成-Mybatis
  20. APP需求调研、对比

热门文章

  1. I/O管理杂记
  2. Splay的基本操作(插入/删除,查询)
  3. qbxt济南七日(游)学习
  4. Kafka Rebalance机制分析
  5. requests.session()会话保持
  6. react + node + express + ant + mongodb 的简洁兼时尚的博客网站
  7. 【Activiti学习之五】BPMN事件
  8. 批处理中setlocal enabledelayedexpansion的作用详细整理
  9. 《Linux就该这么学》培训笔记_ch09_使用ssh服务管理远程主机
  10. QuantLib 金融计算——案例之普通欧式期权分析