前言

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. MySQL数据库操作基础

    .MySQL 是什么? )软件(Software):工具(解决问题) )数据库管理系统(DBMS) )关系型(Relation)数据库管理系统(RDBMS):类似Oracle 扩展:db-engine ...

  2. boost::shared_ptr文档翻译

    shared_ptr: 共享所有权 原文链接 描述 模版类 shared_ptr 存储动态构造对象的指针,通常是由C++ new语句完成的.这个对象指针在最后一个持有指针所有权的shared_ptr被 ...

  3. SI - 系统 - 操作系统简述 (Operating System)

    Unix 操作系统:System V.BSD Microsoft Windows Apple Mac OS Linux FreeBSD 安装 https://jingyan.baidu.com/art ...

  4. Java源码解析——集合框架(三)——Vector

    Vector源码解析 首先说一下Vector和ArrayList的区别: (1) Vector的所有方法都是有synchronized关键字的,即每一个方法都是同步的,所以在使用起来效率会非常低,但是 ...

  5. Python学习:If 语句与 While 语句

    If 语句 用以检查条件:如果条件为真(True),将运行这一块的语句(称作 if-block 或 if 块)    则将运行另一块语句(称作 else-block 或 else 块),其中 else ...

  6. python学习——装饰器函数

    一.装饰器函数的作用是什么 答:装饰器函数是在不修改原函数及其调用方式的情况下对原函数功能进行扩展 对于搞python开发的人来说,函数占据了至关重要的地位.都说学好函数你就可以去找工作了,好了,假如 ...

  7. python 复习函数 装饰器

    # 函数 —— 2天 # 函数的定义和调用 # def 函数名(形参): #函数体 #return 返回值 #调用 函数名(实参) # 站在形参的角度上 : 位置参数,*args,默认参数(陷阱),* ...

  8. A*与IDA*的奇妙之旅

    因为A*卡了一天QAQ 那么,A*是什么呢? A* A*是对于bfs的优化,启发式搜索. 例如下图: 不错,在这张图上,小人去找电脑,用bfs的话: 黄色就是bfs的搜索范围...不要问我为什么选黄色 ...

  9. 函数名前加 & 符号的深入理解 C++

    #include <iostream> using namespace std; int& test_str() { ; return a; //通过返回 a 的地址来进行 值的返 ...

  10. (数据科学学习手札25)sklearn中的特征选择相关功能

    一.简介 在现实的机器学习任务中,自变量往往数量众多,且类型可能由连续型(continuou)和离散型(discrete)混杂组成,因此出于节约计算成本.精简模型.增强模型的泛化性能等角度考虑,我们常 ...