spring-boot 速成(4) 自定义配置

简介: spring-boot 提供了很多默认的配置项,但是开发过程中,总会有一些业务自己的配置项,下面示例了,如何添加一个自定义的配置: 一、写一个自定义配置的类 package com.example.

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

 

参考文章:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-command-line-args

目录
相关文章
|
13天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
16 0
|
6天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
16 0
|
6天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
28 0
|
7天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
19 0
|
7天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
9天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
【Spring系列】Sping VS Sping Boot区别与联系
|
13天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
22 1
|
13天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
19 0
|
13天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
29 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
17天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
14 0