【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- ...
 
随机推荐
- day 014 python 内置函数
			
1. lamda匿名函数2. sorted()3. filter()4. map()5. 递归函数 一 匿名函数(lambda) 函数名= lambda 参数: 返回值 简单算法 a+b 常规算 ...
 - German Collegiate Programming Contest 2013-B:Booking(贪心)
			
Booking Pierre is in great trouble today! He is responsible for managing the bookings for the AC ...
 - How to do distributed locking
			
How to do distributed locking 怎样做可靠的分布式锁,Redlock 真的可行么? 本文是对 Martin Kleppmann 的文章 How to do distribu ...
 - java-SimpleDateFormat类
			
1.SimpleDateFormat类实现日期和字符串的相互转换 * A:DateFormat类的概述 * DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或 ...
 - 通过更改服务器解决双系统ubuntu时间+8
			
安装ntpdate: sudo apt-get install ntpdate 设置校正服务器: sudo ntpdate time.windows.com 设置硬件时间为本地时间: sudo hwc ...
 - Redis(一)入门
			
最近,学习了一下,Redis 这个Nosql数据库,从安装到基本语法,作为入门.下面,整理一下基本知识. 参考的地址如下: http://www.runoob.com/redis/redis-java ...
 - log4net 自定义日志级别记录多个日志
			
程序中原来只记录一个日志,现在我要写一个用户操作日志,需要与原来的日志分开,在config文件中一阵折腾无果(要么写不全,要么写重了,反正没办法完美分离,要么与现存代码没办法完美兼容),差点放弃准备自 ...
 - protobuf   GetExtension
			
get extention values from proto file value, err := proto.GetExtension(imp, openrtb.E_Imp) if err != ...
 - hiveserver 占用内存过大的问题
			
今天为了求解hiveserver占用内存过大的问题,特地加了hive在apache的邮件列表,讨论半天.特别说的是 里面的人确实很热情啊 ,外国人做事确实很认真,讨论帖发的时候都狠详细. 粘出一些记录 ...
 - 04C++const增强、枚举的增强
			
#include <iostream> int main(void) { //const定义常量--->const意味着只读 const int a; int const b; // ...