【spring boot】映射properties文件属性--到Java对象
描述
将*.properties中的内容映射到java对象中;
主要步骤
- 添加 @Component 注解;
- 使用 @PropertySource 注解指定配置文件位置;
- 使用 @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对象的更多相关文章
- Spring boot接受json赋值给java对象
Spring boot接受json赋值给java对象 新建 模板 小书匠 前言 写这个东西,一方面是我自己在做项目的时候,对json的使用还不是十分的熟悉,对spring boot的使用也不是很熟悉, ...
- 【spring boot】使用@Value映射properties文件属性
描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...
- 【转】spring boot application.properties 配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- Spring Boot 环境变量读取 和 属性对象的绑定
网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...
- Spring boot application.properties 配置
原文链接: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.ht ...
- Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题【转】
Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题 http://blog.didispace.com/Spring-Boot-And-Feign- ...
- spring boot 配置文件properties和YAML详解
spring boot 配置文件properties和YAML详解 properties中配置信息并获取值. 1:在application.properties配置文件中添加: 根据提示创建直接创建. ...
- 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 ...
- 启动spring boot项目时报错:java.lang.ClassNotFoundException: javax.servlet.Filter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
随机推荐
- Gym.101908 Brazil Subregional Programming Contest(寒假自训第六场)
这几天睡眠时间都不太够,室友晚上太会折腾了,感觉有点累,所以昨天的题解也没写,看晚上能不能补起来. B . Marbles 题意:给定N组数(xi,yi),玩家轮流操作,每次玩家可以选择其中一组对其操 ...
- 测试那些事儿—BUG
一.作为测试人员,你应该这样报BUG: 不要对程序员说,你的代码有BUG. 他的第一反应是:1.你的环境有问题吧:2.你踏马到底会不会用? 如果你委婉的说:你这个程序和预期的不一样,你看看是不是我的方 ...
- 重绘和回流(reflow和repaint)
由于DOM操作会导致浏览器的回流,回流需要花费大量的时间进行样式计算和节点重绘与渲染,所以应当尽量减少回流次数. 以下是几种常见的减少重绘和回流的方法: 一.不要一项一项的更改页面的样式,尽量一口气写 ...
- hdu 1864 最大报销额 01背包
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 实验吧—隐写术——WP之 SB!SB!SB!
我们先打开解题链接,里面是一张愤怒的小鸟里的小猪~ 既然这是隐写题,那么肯定要把图片下载下来进行分析咯~ 下载下来之后,我们看到题目中提示:LSB 什么是LSB? LSB(Least Signific ...
- 【git】git使用
1.创建github账户 网站:https://github.com/ 注册省略 2.ssk-key客户端配置 作用:不用每次push,clone代码不需要输入用户名+密码 生成ssh-key ssh ...
- 【BZOJ2019】nim
直播写题这刺激233 原题: 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输. ...
- C++编译器报错汇总
1.error: ‘Person’ was not declared in this scope(1)若是一个类或函数的命名空间对使用者不可见(2)成员(静态)函数没有通过对象名或类名进行调用(3)虽 ...
- Unity 3D UI Essentials 学习
Chapter 1: Looking Back,LookingForward Chapter 2: Building Layouts Chapter 3: Control, Control, You ...
- egret学习
1.egret wing4.0不能创建egret游戏项目, 重置了引擎之后就可以了 2.入门介绍:http://developer.egret.com/cn/github/egret-docs/Eng ...