前言

Spring Boot有两中类型的配置文件:.properties和.yml。Spring Boot默认的全局配置文件名为application.properties或者application.yml(spring官方推荐使用的格式是.yml格式,目前官网都是实例都是使用yml格式进行配置讲解的),应用启动时会自动加载此文件,无需手动引入。

方式1:通过配置绑定对象的方式

我自定义一个配置文件my.properties

student.name = ZhangSan
student.age = ${random.int[1,30]}
student.parents[0] = ZhangMing
student.parents[1] = XiaoHua

创建一个Bean,用于自动读取配置文件的内容(注:需保证与配置文件的字段一致)

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import java.util.List; /**
* Created by Administrator on 2018/9/28.
*/
@Component
@PropertySource(value = "classpath:/my.properties", encoding="utf-8") //设置系统加载自定义的配置文件,并指定配置文件编码格式
@ConfigurationProperties(prefix = "student")
@Data
public class Student { private String name; private int age; private List<String> parents;
}
import com.jc.testConf.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by Administrator on 2018/6/26.
*/
@RestController
public class HelloController { @Autowired
Student student; @RequestMapping("/testConf")
private String getStudent(){
return student.toString();
} }

测试,果然能够获取到配置文件的数据



所以,以后,就可通过配置对象来获取相关的配置即可。

方式2:@Value("${blog.author}")的形式获取属性值

import com.jc.testConf.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by Administrator on 2018/6/26.
*/
@RestController
public class HelloController { @Value(value = "${student.name}")
String name; @RequestMapping("/studentName")
private String studentName(){
return name;
} }

测试可以获取

相关说明

在学习配置中,设计到其他的知识点,所有在文章末尾补充下。

注解@Value的说明

有三种使用方式:

  • 基本数值的填充,如:@Value("12")、@Value("张三")
  • 基于SpEl表达式#{} 如上文中年龄也可以这样#{28-2},如:@Value("#{car.luntai}")
  • 基于配置文件${配置文件中参数名},如:@Value("${database.source.url}")

参考

https://blog.lqdev.cn/2018/07/14/springboot/chapter-third/

http://blog.51cto.com/4247649/2118348

Spring Boot学习(二):配置文件的更多相关文章

  1. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  2. Spring boot 学习二:入门

    1: 需要的环境: JDK:至少JDK7才支持Spring boot maven:至少3.2 spring-boot:1.2.5.RELEASE(在pom.xml中指定) 2: 创建一个maven工程 ...

  3. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  4. 我的Spring Boot学习记录(二):Tomcat Server以及Spring MVC的上下文问题

    Spring Boot版本: 2.0.0.RELEASE 这里需要引入依赖 spring-boot-starter-web 这里有可能有个人的误解,请抱着怀疑态度看. 建议: 感觉自己也会被绕晕,所以 ...

  5. Spring Boot学习路线

    Spring Boot 学习路线,本文计划根据作者近几年的工作.学习经验,来分析和制定一个学习使用 Spring Boot技术的步骤路线图. SpringBoot是伴随着Spring4.0诞生的: S ...

  6. Spring Boot学习(三)解析 Spring Boot 项目

    一.解析 pom.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...

  7. Spring Boot学习大全(入门)

    Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...

  8. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  9. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  10. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

随机推荐

  1. [Windows]ping itsafe&环境变量

    (1)when you ping a computer from itsafe,the ping command should return the local IP address. (2)wind ...

  2. CALayer简介(转)

    一.简单介绍  在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮,一个文本标签,一个文本输入框,一个图标等等,这些都是UIView.  其实UIView之所以能显示在屏幕上,完全 ...

  3. TortoiseSVN SendRpt.exe not found解决方案

    重启了Explorer.exe即可.这里也补充下简单的重启Explorer.exe的方法:打开任务管理器,找到“Windows资源管理器”,右键--重新启动. 或者,右键--结束任务,然后点击 文件- ...

  4. Java Iterator ListIterator 理解

    一. Iterator 常用操作 next hasNext remove 先上源码:JDK8 简化版本,用于说明问题 private class Itr implements Iterator< ...

  5. C++调用WMI类查询获取操作系统名

    #define _WIN32_DCOM #include <iostream> #include <comdef.h> #include <Wbemidl.h> u ...

  6. python核心编程2 第十四章 练习

    14-3.执行环境.创建运行其他Python脚本的脚本. if __name__ == '__main__': with open('test.py') as f: exec(f.read()) 14 ...

  7. 配置Github秘钥

    Git安装完成后,需要手动配置ssh密钥 配置github的ssh密钥: (1)打开Git Bash查看电脑上是否已经存在SSH密钥: 输入 cd ~/.ssh 若如上图显示无法找到该文件则要创建新的 ...

  8. vue组件的基本知识点

    1. 组件中 is 的特性: 有些 HTML 元素,诸如 <ul>.<ol>.<table> 和 <select>,对于哪些元素可以出现在其内部是有严格 ...

  9. laravel 基础 --内置函数

    简介 Laravel 自带了一系列 PHP 辅助函数,很多被框架自身使用,如果你觉得方便的话也可以在代码中使用它们. https://laravelacademy.org/post/8967.html ...

  10. 03 mysql补充 (进阶)

    增加字段 alter table tb1 add age int first; 增加到第一 alter table tb1 add sex int after id; # 改变位置,id是字段名字 a ...