描述

将*.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. SQLI DUMB SERIES-3

    less3 输入?id=1' 说明输入的id旁边加了单引号和括号('id'),直接在1后面加入“ ') ”,闭合前面的单引号和括号. 方法同less1相同. 例如:查询PHP版本和数据库名字 ?id= ...

  2. Kafka设计解析:Kafka High Availability

    Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则宕机期间其上所有Partition都无法继续提供服务.若该Broker永远不能再恢复,亦 ...

  3. (0)diango、ORM的语法

    Django PS:Django默认的是sqlite3数据库 PS:settings里面的这里可以修改数据库 1.^ 这个符号就是以什么开头   #url(r'index/',views.index) ...

  4. python基础-函数基本特性和用法

    函数: 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因变量,y是x的函数.自变量x的取值 ...

  5. AangularJS入门总结三

    (参考的资料) 1. 数据绑定的原理: (1)  $watch 队列: 在DOM中每次绑定一些东西,就会往$watch队列中插入一条$watch: 每一个绑定到了DOM上的数据都会生成一个$watch ...

  6. 尚硅谷【SpringBoot】web(源码讲解太多不建议阅读)

    四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...

  7. Java基础二(变量、运算符)

    1.变量2.运算符 ###01变量概述 * A: 什么是变量? * a: 变量是一个内存中的小盒子(小容器),容器是什么?生活中也有很多容器,例如水杯是容器,用来装载水:你家里的大衣柜是容器,用来装载 ...

  8. ES5新增数组方法

    forEach/map   every/some  indexOf/lastIndexOf  filter reduce Array.isArray

  9. ArrayList集合方法

  10. keil5配置ST Link v2 for STM32F10x

    请务必先安装MDKCM525.EXE,否则会找不到闪存编程算法