描述

将*.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. 用jq修改css

    $(".tag_add").css("background","#ffffff"); $(".tag_add").css ...

  2. Codeforces 520B:Two Buttons(思维,好题)

    题目链接:http://codeforces.com/problemset/problem/520/B 题意 给出两个数n和m,n每次只能进行乘2或者减1的操作,问n至少经过多少次变换后能变成m 思路 ...

  3. 质因子分解(Pollard_Rho法)

    LL Pollard_Rho(LL n, LL c) { LL x, y, d; LL i = , k = ; x = y = rand() % n; do { i++; d = gcd(n + y ...

  4. 杜教BM

    #include <algorithm> #include <iterator> #include <iostream> #include <cstring& ...

  5. mysql对后空格不敏感 mysql数据库对空格的查询处理

    mysql对后空格不敏感 https://blog.csdn.net/lzupb/article/details/73530589 结论:查询条件中建议对字符串做trim处理,在数据入库的时候最好也做 ...

  6. 有关O_APPEND标志和lseek()的使用

    编程之路刚刚开始,错误难免,希望大家能够指出. O_APPEND表示以每次写操作都写入文件的末尾.lseek()可以调整文件读写位置. <<Linux/UNIX系统编程手册>> ...

  7. 从简单的mongodb example 的观察

    https://github.com/no7dw/mongodb-example 这是最基础的连接查询.(branch master) var MongoClient = require('mongo ...

  8. vorpal 又一个方便的cli 开发包

    vorpal 是一个npm 包,我们可以用来开发专业的cli 程序 简单使用 初始化项目 yarn init -y 添加依赖 yarn add vorpal 简单demo app.js // cons ...

  9. Debug outlook add-in (office.js) 小技巧

    这几天在使用office.js 做outlook add-in的时候出现了一个问题: 不知道运行时去调试. 这里给大家介绍两个调试add-in 的方法. office365 其他软件 add-ins ...

  10. 最大值最小值(max,max_element)

    min 如果比不出大小就返回第一个引数 //版本一:调用operator< template <class LessThanComparable> const LessThanCom ...