spring-boot 速成(4) 自定义配置
spring-boot 提供了很多默认的配置项,但是开发过程中,总会有一些业务自己的配置项,下面示例了,如何添加一个自定义的配置:
一、写一个自定义配置的类
package com.example.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* Created by 菩提树下的杨过 on 2017/4/15.
*/
@Data
@Component
@ConfigurationProperties(prefix = "web.config")
public class WebConfig {
private String webTitle;
private String authorName;
private String authorBlogUrl;
}
注意上面的注解@ConfigurationProperties(prefix = "web.config"),这表示这个类将从属性文件中读取web.config开头的属性值
二、在application.yml中配置属性
spring-boot支持properties及yml格式,不过推荐大家使用新的yml格式,看上去更清晰
web:
config:
webTitle: "欢迎使用SpringBoot"
authorName: "菩提树下的杨过"
authorBlogUrl: "http://yjmyzz.cnblogs.com/"
三、来一发
为了演示效果,可以弄一个最简单的web应用,先来一个controller
package com.example.controllers; import com.example.config.WebConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class IndexController { @Autowired
WebConfig webConfig; @RequestMapping("/")
String index(ModelMap map) {
map.addAttribute("title", webConfig.getWebTitle());
map.addAttribute("name", webConfig.getAuthorName());
map.addAttribute("blog", webConfig.getAuthorBlogUrl());
return "index";
}
}
然后在index.html模板中写点东西(注:本例使用了thymeleaf做为模板引擎)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${title}"></title>
</head>
<body>
<div>
<h1 th:text="${name}"/>
<h1 th:text="${blog}"/>
</div>
</body>
</html>
最后跑起来的运行效果如下:

四、配置文件的加载顺序
把所有配置全都打在一个jar包里,显然不是最好的做法,更常见的做法是把配置文件放在jar包外面,可以在需要时,不动java代码的前提下修改配置,spring-boot会按以下顺序加载配置文件application.properties或application.yml:
4.1 先查找jar文件同级目录下的 ./config 子目录 有无配置文件 (外置)
4.2 再查找jar同级目录 有无配置文件(外置)
4.3 再查找config这个package下有无配置文件(内置)
4.4 最后才是查找classpath 下有无配置文件(内置)
附:源代码下载 spring-boot-web-demo.zip
参考文章:
spring-boot 速成(4) 自定义配置的更多相关文章
- Spring Boot 2 使用自定义配置
在application.yml定义配置后,可以使用Environment来读取配置,也可以使用@Value注解让业务代码去读取配置.如果属性较多,可以定义属性映射对象. 开发环境:IntelliJ ...
- spring boot加载自定义配置
1.通过@Value 配置文件中 wechat: ssh: host: 192.0.1.1 port: 22 加载类 @Component @Data public class SftpConfig ...
- spring boot 配置文件提示自定义配置属性
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot之实现自动配置
GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...
- spring boot(5)-properties参数配置
application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...
- Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置
Actuator 简介 Actuator 是 Spring Boot 提供的对应用系统的自省和监控功能.通过 Actuator,可以使用数据化的指标去度量应用的运行情况,比如查看服务器的磁盘.内存.C ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- spring boot / cloud (四) 自定义线程池以及异步处理@Async
spring boot / cloud (四) 自定义线程池以及异步处理@Async 前言 什么是线程池? 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线 ...
- Spring Boot 2.0 教程 | 配置 Undertow 容器
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...
随机推荐
- 从简单类型到复杂类型的参数传递用例,以及传递简单string类型的解决办法
一,简单类型的传值 比如 public Users Get(int id) ,它可以使用两种方式获取: api/default/5 $.get("/api/default" ...
- 雨林木风ghostwin7纯净版系统下载
雨林木风ghostwin7纯净版系统下载 关于easyuidatagrid的问题,跪求老司机带带我..... 关于cst_modesys/stat.h一个问题求解答谢谢 [程序]STM32使用SPI接 ...
- Angular 下的 directive (part 2)
ngCloak ngCloak指令被使用在,阻止angular模板从浏览器加载的时候出现闪烁的时候.使用它可以避免闪烁问题的出现. 该指令可以应用于<body>元素,但首选使用多个ng ...
- 关于升级程序版本时version与build修改的问题
CHENYILONG Blog 关于升级程序版本时version与build修改的问题 #问题#从V1.0升级到V1.0.1.version是一定要改的,那么build需要修改吗? #解答#一般习惯上 ...
- HDU 1251 统计难题 字符匹配
题目描述:先说明此题只有一个测试实例,然后输入一系列的单词,以一个回车为结束符,然后输入一个字符串,要你查找以这个字符串为前缀的单词的个数,处理到文件结束. 解题报告:一开始看到这题,竟然直接用暴力去 ...
- Python2的object和type
前言: Python在2.2和3.0之间,把继承了object的类叫做新式类,如果我们定义了一个类,他没有继承object,则不是新式类,则没有__class__,__bases__等属性,而用typ ...
- ioctl函数详细说明(网络)
ioctl 函数 本函数影响由fd 参数引用的一个打开的文件. #include<unistd.h> int ioctl( int fd, int request, .../* void ...
- linux下查看各硬件型号
查看主板型号 # dmidecode |grep -A 8 "System Information"System Information 上网查DELL CS24-TY,找到说主板 ...
- Gentoo rc-update service ‘net.eth0′ does not exist
最近迷上了Gentoo,并相信以后也会把更多的精力放在Gentoo上,不过Gentoo的安装的过程的确让很多人却步. 本文只提到添加net.eth0到默认的运行级别时一个很小的报错解决. # nano ...
- ckeditor:基本使用方法
引用网址:http://blog.sina.com.cn/s/blog_6961ba9b0102wwye.html 1.获得值 var editor=CKEDITOR.replace( 'editor ...