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

目录
相关文章
|
6天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
13 0
|
21天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
38 0
|
1月前
|
Java Spring 容器
【Java】Spring如何扫描自定义的注解?
【Java】Spring如何扫描自定义的注解?
34 0
|
28天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
2天前
|
XML Java C++
【Spring系列】Sping VS Sping Boot区别与联系
【4月更文挑战第2天】Spring系列第一课:Spring Boot 能力介绍及简单实践
23 0
【Spring系列】Sping VS Sping Boot区别与联系
|
6天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
13 1
|
6天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
15 0
|
6天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
16 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
10天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
12 0
|
28天前
|
Java 数据库连接 Spring
Spring5深入浅出篇:Spring配置⽂件参数化
该文档介绍了Spring配置文件参数化的概念和步骤。目的是将经常需要修改的配置,如数据库连接参数,从主配置文件中分离到一个单独的`.properties`文件,以便于管理和维护。步骤包括创建小型配置文件(如`db.properties`),在`applicationContext.xml`中引入该文件,并使用`${key}`语法在Spring配置文件中引用这些参数。最终通过测试验证配置文件参数化的成功。