【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- ...
随机推荐
- flask中的request和response
1.request 1.request的常用成员 1.scheme : 获取请求方案(协议) 2.method : 获取本期请求的请求方式(重点) 3.args : 获取使用get请求方式提交的数据 ...
- Unity3d mesh合并,网格合并具体用法教程
Unity开发Mesh合并网格 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...
- dp——poj1088(Description)
dp可以按照思想大致分为两种,一种是递推的形式,一种是递归的形式(记忆化搜素). 比如求这个题因为无法知道从哪个点开始,所以只能用递归的形式,因为有一个回溯的过程. 但是很多题目既可以用递推也可以用递 ...
- 1100C NN and the Optical Illusion
推公式,水题.cos()函数是默认弧度制的 #include <iostream> #include <cstring> #include <string> #in ...
- 大整数四则运算------(c++ 实现 乘法没有用傅里叶变换)
/* 优点: 1 支持负整数的运算 2 良好的输出形式 没有前导零 3 支持cin直接输入 支持cout直接输出 4 支持整数的直接赋值 big_int x=100; 缺点: 1 封装不好 基本都是友 ...
- Codeforces 520B:Two Buttons(思维,好题)
题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...
- zabbix使用自定义key进行监控
我的zabbix-server是安装在另一台虚拟机上的,用来监控下图中的这台虚拟机 先修改zabbix的客户端配置文件,增加UserParameter那行,这里我只是用来测试,所以就随便起了一个名为p ...
- scanf() gets() fgets()使用注意事项
1.scanf() 遇到'\n'停止从输入缓冲区中接收,接收完后‘\n’还存在于缓冲区中.当输入的字符数少于缓冲区大小时,字符后面有自动补上‘\0’,当输入字符大于缓冲区时,也直接拷贝到缓冲中,因此缓 ...
- MySQL Lock--并发插入导致的死锁
============================================================================ 测试脚本: 表结构: CREATE TABLE ...
- awesome-workflow-engines
转自:https://github.com/meirwah/awesome-workflow-engines awesome-workflow-engines A curated list of aw ...