描述

将*.properties中的内容映射到java对象中;

主要步骤

  1. 添加 @Component 注解;
  2. 使用 @PropertySource 注解指定配置文件位置;
  3. 使用 @ConfigurationProperties 注解,设置相关属性;

my.properties

author.name=ssslinppp
author.age=128
author.student.name=studentName
author.student.age=88

AuthorSettings.java

package com.sssppp;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "author")
@PropertySource("classpath:my.properties")
public class AuthorSettings {
private String name;
private Long age;
private Student student; public Student getStudent() {
return student;
} public void setStudent(Student student) {
this.student = student;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Long getAge() {
return age;
} public void setAge(Long age) {
this.age = age;
}
}

Student.java

package com.sssppp;

public class Student {
private String name; private int age; public Student() {
super();
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
} }

测试类

package com.sssppp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class Ch623 { @Autowired
private AuthorSettings authorSettings; @RequestMapping("/ch623")
public AuthorSettings index() {
return authorSettings;
}
}

测试结果

{"name":"ssslinppp","age":128,"student":{"name":"studentName","age":88}}

【spring boot】映射properties文件属性--到Java对象的更多相关文章

  1. Spring boot接受json赋值给java对象

    Spring boot接受json赋值给java对象 新建 模板 小书匠 前言 写这个东西,一方面是我自己在做项目的时候,对json的使用还不是十分的熟悉,对spring boot的使用也不是很熟悉, ...

  2. 【spring boot】使用@Value映射properties文件属性

    描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...

  3. 【转】spring boot application.properties 配置参数详情

    multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...

  4. Spring Boot 环境变量读取 和 属性对象的绑定

    网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...

  5. Spring boot application.properties 配置

    原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...

  6. Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题【转】

    Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题 http://blog.didispace.com/Spring-Boot-And-Feign- ...

  7. spring boot 配置文件properties和YAML详解

    spring boot 配置文件properties和YAML详解 properties中配置信息并获取值. 1:在application.properties配置文件中添加: 根据提示创建直接创建. ...

  8. Inspection info: Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and in

    Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection info: Checks Spring Boot applicati ...

  9. 启动spring boot项目时报错:java.lang.ClassNotFoundException: javax.servlet.Filter

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

随机推荐

  1. C++学习(五)(C语言部分)之 运算符

    运算符学习时的笔记(其实也没什么用,留着给自己看的) 运算符 用来对数据运算的符号 优先级 3+4*5+6 先乘除 然后加减 运算符优先级高就先算 40多个运算符 15层优先级 不需要背 1.查表 2 ...

  2. vue项目的创建

    https://jingyan.baidu.com/article/456c463b359e630a5931444e.html

  3. pycharm汉化 (ubuntu版)

    终端依次输入 cd  /tmp git clone https://github.com/ewen0930/PyCharm-Chinese cd Pycharm-Chinese bash packag ...

  4. UVA10341 Solve It

    题意 PDF 分析 在\(0\le x\le 1\)时,\(f(x)=pe^{-x}+q\sin x+r\cos x+s\tan x+tx^2+u\)是减函数,所以当\(f(0)\ge 0 \wedg ...

  5. CH#46A 磁力块

    题意 磁力块 CH Round #46 - 「Adera 8」杯NOI模拟赛 描述 在一片广袤无垠的原野上,散落着N块磁石.每个磁石的性质可以用一个五元组(x,y,m,p,r)描述,其中x,y表示其坐 ...

  6. set集合的遍历(基于迭代器和增强for循环,没有一般的for循环)

    赋:开发项目中见到的代码 Java中Set集合是一个不包含重复元素的Collection,首先我们先看看遍历方法 package com.sort; import java.util.HashSet; ...

  7. Stateful Kubernetes Applications Made Easier: PSO and FlashBlade

    转自:https://medium.com/@joshua_robinson/stateful-kubernetes-applications-made-easier-pso-and-flashbla ...

  8. 使用skipper 扩展fabio 的路由&&http proxy 功能

    skipper 具有强大的http 路由功能,fabio 是一个方便的基于consul 的负载均衡软件, 我们可以方便的使用skipper 路由功能进行fabio的扩展,使用registrator 进 ...

  9. div中 li宽度不固定 ie6和ie7不兼容不自动换行

    我的li因为内容字数不一样,所以宽度不固定,给他float:left属性后,ie6和ie7不兼容,不自动换行!我给ul或者li: ul{white-space: nowrap} 属性还是不管用..最后 ...

  10. Linux常用文本操作命令整理

    阅读目录 1.统计命令——wc 2.切分命令——cut 3.排序命令——sort 4.去重命令——uniq 5.查找命令——grep 6.替换/查找/删除命令——sed 7.强大的文本分析命令——aw ...