阅读此文章之前,需要参考 https://www.cnblogs.com/mysummary/p/12238242.html 创建Spring Boot项目

建成后目录如下

一、在com.demo.springdemo包下新建两个类,bean.Dog 和 bean.Person 类(作测试用)

  1. Person类代码如下

     public class Person {
    
         //person的基本属性
    private String lastname;
    private Integer age;
    private Boolean isboss;
    private Date birth; //map和list类型属性
    private Map<String,Object> maps;
    private List<Object> lists; //对象属性
    private Dog dog; }
  2. Dog类代码如下
     public class Dog {
    private String name;
    private Integer age;
    }
  3. 在Person类和Dog类下分别使用⌘+N 快捷键调出 toString() 方法
  4. 在Person类和Dog类下分别使用⌘+N 快捷键调出 Getter and Setter 方法   【类中变量属性一般为private。这里之所以将变量属性设为private是为了引用该类时保证数据被其他类引用,提高数据的安全性。结合java的封闭性和安全性,不难看出这样这些变量按道理来说像是被封闭在该类里,为了引用这些变量,这里我们使用了set和get方法去操作变量(get用来取值,set用来修改变量值),既能提高封装型,也不失安全性】
  5. 完成后的代码如下(以Dog类为例)
     public class Dog {
    private String name;
    private Integer age; @Override
    public String toString() {
    return "Dog{" +
    "name='" + name + '\'' +
    ", age=" + age +
    '}';
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public Integer getAge() {
    return age;
    } public void setAge(Integer age) {
    this.age = age;
    }
    }

二、在pom文件中导入配置文件处理器

* 导入配置文件处理器

<!-- 导入配置文件处理器,配置文件进行绑定就会有提示,重新运行程序后生效 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

* 重新运行主程序即可生效

三、在application.properties文件中写入配置内容

 person.lastname=张三
person.age=18
person.birth=2017/12/15
person.isboss=false person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c person.dog.name=dog_one
person.dog.age=2

四、加入相关注解,获取配置文件中的内容

  • 在Person.java文件中加入 @ConfigurationProperties(prefix = "person")注解
  • 再加上 @Component注解

五、下面来测试一下是否能获取到配置文件中的内容

  • 打开Spring Booot的单元测试文件

  • 在文件下先添加注解

    @RunWith(SpringRunner.class)
     (若出现无法添加该注解,参考https://blog.csdn.net/weixin_43681796/article/details/98033592操作即可)
  • 注入Person:

    @Autowired
    Person person; //注入person
  • 在控制台添加一个输出person
    System.out.println(person);

完成后代码如下:

 @SpringBootTest
class SpringdemoApplicationTests { @Autowired
Person person; //注入person
@Test
void contextLoads() {
System.out.println(person);
}
}
  • 运行测试程序,在控制台输出获取结果

可以看出来,输出的 lastname='å¼ ä¸‰' 出现乱码现象

解决方法:在performance中查找 file encodings,按照下图配置

注意!!!完成后,需要删除原来的application.properties文件,重新配置,再次运行测试程序,可打印出正确结果

Spring Boot框架 - application.properties配置的更多相关文章

  1. spring boot配置文件application.properties配置JPA以及数据源

    1.application.properties配置jpa模板 spring.datasource.url=jdbc:mysql://localhost:3306/springboottest?use ...

  2. spring boot 使用application.properties 进行外部配置

    application.properties大家都不陌生,我们在开发的时候,经常使用它来配置一些可以手动修改而且不用编译的变量,这样的作用在于,打成war包或者jar用于生产环境时,我们可以手动修改环 ...

  3. Spring Boot2 系列教程(四)理解Spring Boot 配置文件 application.properties

    在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml . 虽然 properties 文件比较常见,但是相对于 properties 而言,ya ...

  4. Spring Boot 项目 application.properties配置说明

    #======================================================================================# ★☆★☆★☆★☆★☆ ...

  5. Spring boot 的application.properties 全局配置

    端口号.项目名称 application.properties: server.port=8888 server.context-path=/start 日志相关的配置 # 自定义日志配置路径 log ...

  6. Spring Boot 框架学习 (一)配置并运行Spring Boot 框架

    下载开发工具: 下载完成打开以后,第一步检查环境 查看jdk是否配置: 接着一定要注意,maven通常情况下它是没有给你配置的,要自行配置: 右键新建: 然后依赖选择web.跟Mybatis就行了. ...

  7. Spring Boot Dubbo applications.properties 配置清单

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 与其纠结,不如行动学习.Innovate ,And out execute ! 』 本文 ...

  8. spring boot +mybatis(通过properties配置) 集成

    注:日常学习记录贴,下面描述的有误解的话请指出,大家一同学习. 因为我公司现在用的是postgresql数据库,所以我也用postgresql进行测试 一.前言 1.Spring boot 会默认读取 ...

  9. Spring boot配置文件application.properties和bootstrap.properties的区别

    spring boot 有两种配置文件 (1)application.properties(application.yml) 系统级别的一些参数配置,这些参数一般是不会变动的 (2)bootstrap ...

随机推荐

  1. Python中读取目录里的文件并按排序列出

    1.os.listdir():用于返回指定的文件夹包含的文件或文件夹的名字的列表. 如: dir ='F:/Home_01/img'#当前目录 filenames=os.listdir(dir)#fi ...

  2. Mike and strings

    Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can ...

  3. [Codechef - ADITREE] Adi and the Tree - 树链剖分,线段树

    [Codechef - ADITREE] Adi and the Tree Description 树上每个节点有一个灯泡,开始所有灯泡都是熄灭的.每次操作给定两个数 \(a,b\) ,将 \(a,b ...

  4. valign

    值 描述 top 对内容进行上对齐. middle 对内容进行居中对齐(默认值). bottom 对内容进行下对齐. baseline 与基线对齐.

  5. 220. 存在重复元素 III

    题目: 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. 示例 1: ...

  6. 第三篇,ajax 和 axios、fetch的区别

    1.jQuery ajax $.ajax({ type: 'POST', url: url, data: data, dataType: dataType, success: function () ...

  7. 《CSS揭秘》》

    1,透明边框 默认状态下,背景会延伸到边框区域的下层.这样就在半透明的黑色边框中透出了这个容器自己的纯白色背景. 谢天谢地,从w3c的背景与边框第三版开始,我们可以通过 background-clip ...

  8. spring自动装配和通过java实现装配

    1.组建扫描 在类上添加注解@Component注解可以实现组建扫描 @Component public class A{ ... } 2.自动装配 通过在属性上或者方法上添加@Autowired注解 ...

  9. 如何转proto

    找到对应协议在对应proto中的片段(片段的子也要提取),提出来 放到适合proto文件中(逻辑 || 功能近似协议聚集地) 转换成proto.js 替换 || 增加原有js内容

  10. Java + Selenium 无头浏览器模式

    我们说的无头模式,只是在爬虫执行的时候,不再弹出浏览器的界面,只是使用浏览器的内核进行爬取,下面是示例代码: //设置本地chromedriver地址 System.setProperty(" ...