Spring Boot学习(二):配置文件
前言
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学习(二):配置文件的更多相关文章
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- Spring boot 学习二:入门
1: 需要的环境: JDK:至少JDK7才支持Spring boot maven:至少3.2 spring-boot:1.2.5.RELEASE(在pom.xml中指定) 2: 创建一个maven工程 ...
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- 我的Spring Boot学习记录(二):Tomcat Server以及Spring MVC的上下文问题
Spring Boot版本: 2.0.0.RELEASE 这里需要引入依赖 spring-boot-starter-web 这里有可能有个人的误解,请抱着怀疑态度看. 建议: 感觉自己也会被绕晕,所以 ...
- Spring Boot学习路线
Spring Boot 学习路线,本文计划根据作者近几年的工作.学习经验,来分析和制定一个学习使用 Spring Boot技术的步骤路线图. SpringBoot是伴随着Spring4.0诞生的: S ...
- Spring Boot学习(三)解析 Spring Boot 项目
一.解析 pom.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- Spring Boot学习大全(入门)
Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
随机推荐
- iOS | 地图定位
在IOS开发中,最常见的功能之一就是地图定位功能,不单单是百度地图,高德地图等专业的地图导航软件,还有美团,咕咚等一些美食购物类和运动类也需要这样的功能,所以学会这项技能是一名IOS开发工程师必须的. ...
- 对AFNetworking的二次封装
HttpTool.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef void(^HttpS ...
- MySql基本数据类型及约束
1. 常用的数据类型(data_type) 字符串类型 CHAR(n) : 固定长度 VARCHAR(n) : 可变长度 NCHAR(n) : 使用utf8存储,固定长度 NVARCHAR(n) : ...
- 查找mysql中未提交的事务
1.查找未提交事务 在mysql中运行: select t.trx_mysql_thread_id from information_schema.innodb_trx t 2.删除线程 kill ...
- MySQL学习路线图
- python应用:经纬度匹配
需要安装第三方包:requests 本次经纬度匹配采用高德地图api,首先将gps坐标转化为高德地图的经纬度坐标,然后再根据转化后的坐标进行匹配. 本次匹配主要是获取距离给定经纬度最近的poi点地址信 ...
- 转载:小白使用eclipse提交到GitHub (详细步骤)
本篇文章只是备忘,以防电脑重装找不到记录 教程:https://blog.csdn.net/bendanany/article/details/78891804
- 数字滤波器的MATLAB与FPGA实现--Altera/Verilog版的pdf版,杜勇等编著的书。
自己在网上找了很久才找到的资源,花了很大的劲,觉得不易,特地分享给大家.本书讲了使用FPGA的Fir IIR IP核与Matlab配合使用生成滤波器的详细使用方法.贴出地址,http://downlo ...
- 002---tcp/ip五层详解
tcp/ip 五层模型讲解 越靠底层就越接近硬件,越靠上层越接近用户.先从底层看起,理解整个互联网通信的原理. 物理层(传输电信号) 孤立的计算机想要一起玩.就必须用硬件在计算机之间完成组网.以硬件做 ...
- Git 克隆指定分支代码
git clone 指定分支 拉代码 1.git clone 不指定分支 git clone http://10.1.1.11/service/sz-service.git 2.git clone ...