一块儿来学SpringBoot | 第二篇:SpringBoot配置详解

SpringBoot 是为了简化 Spring 应用的建立、运行、调试、部署等一系列问题而诞生的产物, 自动装配的特性让咱们能够更好的关注业务自己而不是外部的XML配置,咱们只需遵循规范,引入相关的依赖就能够轻易的搭建出一个WEB工程java

上一篇介绍了 SpringBoot 由来及构建方式,经过第一章的教程咱们对 SpringBoot 不在感到陌生,能够发现 SpringBoot 虽然干掉了 XML 但未作到 零配置,它体现出了一种 约定优于配置,也称做按约定编程,是一种软件设计范式,旨在减小软件开发人员需作决定的数量,得到简单的好处,而又不失灵活性。 通常状况下默认的配置足够知足平常开发所需,但在特殊的状况下,咱们每每须要用到自定义属性配置、自定义文件配置、多环境配置、外部命令引导等一系列功能。不用担忧,这些 SpringBoot 都替咱们考虑好了,咱们只须要遵循它的规则配置便可git

准备前提github

为了让 SpringBoot 更好的生成数据,咱们须要添加以下依赖(该依赖能够不添加,可是在 IDEA 和 STS 中不会有属性提示,没有提示的配置就跟你用记事本写代码同样苦逼,出个问题弄哭你去),该依赖只会在编译时调用,因此不用担忧会对生产形成影响...web

  
    
  
  
  
   
   
            
   
   
  1. spring

  2. 数据库

  3. 编程

  4. 浏览器

  5. 微信

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-configuration-processor</artifactId>    <optional>true</optional></dependency>

自定义属性配置

在 application.properties 写入以下配置内容app

  
    
  
  
  
   
   
            
   
   
my1.age=22my1.name=battcn

其次定义 MyProperties1.java 文件,用来映射咱们在 application.properties 中的内容,这样一来咱们就能够经过操做对象的方式来得到配置文件的内容了

  
    
  
  
  
   
   
            
   
   
package com.battcn.properties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * @author Levin * @since 2018/4/23 0023 */@Component@ConfigurationProperties(prefix = "my1")public class MyProperties1 {    private int age;    private String name;    // 省略 get set    @Override    public String toString() {        return "MyProperties1{" +                "age=" + age +                ", name='" + name + '\'' +                '}';    }}

接下来就是定义咱们的 PropertiesController 用来注入 MyProperties1 测试咱们编写的代码,值得注意的是 Spring4.x 之后,推荐使用构造函数的形式注入属性...

  
    
  
  
  
   
   
            
   
   
import com.battcn.properties.MyProperties1;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author Levin * @since 2018/4/23 0023 */@RequestMapping("/properties")@RestControllerpublic class PropertiesController {    private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);    private final MyProperties1 myProperties1;    @Autowired    public PropertiesController(MyProperties1 myProperties1) {        this.myProperties1 = myProperties1;    }    @GetMapping("/1")    public MyProperties1 myProperties1() {        log.info("=================================================================================================");        log.info(myProperties1.toString());        log.info("=================================================================================================");        return myProperties1;    }}

打开浏览器,输入以下地址: http://localhost:8080/properties/1,观察控制台,监听到以下内容则表示程序正确

  
    
  
  
  
   
   
            
   
   
2018-04-23 15:51:43.145  INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController      : =================================================================================================2018-04-23 15:51:43.145  INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController      : MyProperties1{age=22, name='battcn'}2018-04-23 15:51:43.145  INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController      : =================================================================================================

自定义文件配置

定义一个名为 my2.properties 的资源文件,自定义配置文件的命名不强制 application 开头

  
    
  
  
  
   
   
            
   
   
my2.age=22my2.name=Levinmy2.email=1837307557@qq.com

其次定义 MyProperties2.java 文件,用来映射咱们在 my2.properties 中的内容。

  
    
  
  
  
   
   
            
   
   
package com.battcn.properties;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * @author Levin * @since 2018/4/23 0023 */@Component@PropertySource("classpath:my2.properties")@ConfigurationProperties(prefix = "my2")public class MyProperties2 {    private int age;    private String name;    private String email;    // 省略 get set    @Override    public String toString() {        return "MyProperties2{" +                "age=" + age +                ", name='" + name + '\'' +                ", email='" + email + '\'' +                '}';    }}

接下来在 PropertiesController 用来注入 MyProperties2 测试咱们编写的代码

  
    
  
  
  
   
   
            
   
   
@GetMapping("/2")public MyProperties2 myProperties2() {    log.info("=================================================================================================");    log.info(myProperties2.toString());    log.info("=================================================================================================");    return myProperties2;}

打开浏览器,输入以下地址: http://localhost:8080/properties/2,观察控制台,监听到以下内容则表示程序正确

  
    
  
  
  
   
   
            
   
   
2018-04-23 15:59:45.395  INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController      : =================================================================================================2018-04-23 15:59:45.395  INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController      : MyProperties2{age=22, name='Levin', email='1837307557@qq.com'}2018-04-23 15:59:45.395  INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController      : =================================================================================================

多环境化配置

在真实的应用中,经常会有多个环境(如:开发,测试,生产等),不一样的环境数据库链接都不同,这个时候就须要用到 spring.profile.active 的强大功能了,它的格式为 application-{profile}.properties,这里的 application 为前缀不能改, {profile} 是咱们本身定义的。

建立 application-dev.properties、 application-test.properties、 application-prod.properties,内容分别以下

application-dev.properties

  
    
  
  
  
   
   
            
   
   
server.servlet.context-path=/dev

application-test.properties

  
    
  
  
  
   
   
            
   
   
server.servlet.context-path=/test

application-prod.properties

  
    
  
  
  
   
   
            
   
   
server.servlet.context-path=/prod

在 application.properties 配置文件中写入 spring.profiles.active=dev,这个时候咱们在次访问 http://localhost:8080/properties/1 就没用处了,由于咱们设置了它的 context-path=/dev,因此新的路径就是 http://localhost:8080/dev/properties/1 ,由此能够看出来咱们激活不一样的配置读取的属性值是不同的

外部命令引导

前面三种方式都是基于配置文件层面的,那么有没有办法外部引导呢,假设这样的场景,咱们对已经开发完成的代码打包发布,期间在测试环境测试经过了,那么便可发布上生产,这个时候是修改 application.properties的配置方便仍是直接在命令参数配置方便呢,毫无疑问是后者更有说服力。默认状况下, SpringApplication 会将命令行选项参数(即:--property,如--server.port=9000)添加到Environment,命令行属性始终优先于其余属性源。

如何测试?

  • 进入到项目目录,此处以我本地目录为主:F:/battcn-workspace/spring-boot2-learning/chapter2

  • 而后打开 cmd 程序,不会在当前目录打开 cmd 的请自行百度,输入: mvnpackage

  • 打包完毕后进入到:F:/battcn-workspace/spring-boot2-learning/chapter2/target 目录中去,咱们能够发现一个名为chapter2-0.0.1-SNAPSHOT.jar 的包

  • 接着在打开 cmd 程序,输入: java-jar chapter2-0.0.1-SNAPSHOT.jar--spring.profiles.active=test--my1.age=32。仔细观察 spring.profiles.active=test、 my1.age=32 这俩配置的键值是否是似曾相识(不认识的请从开头认真阅读)

  • 最后输入测试地址:http://localhost:8080/test/properties/1 咱们能够发现返回的JSON变成了 {"age":32,"name":"battcn"} 表示正确

总结

  • 掌握 @ConfigurationProperties、 @PropertySource 等注解的用法及做用

  • 掌握编写自定义配置

  • 掌握外部命令引导配置的方式

目前不少大佬都写过关于 SpringBoot 的教程了,若有雷同,请多多包涵,本教程基于最新的 spring-boot-starter-parent2.0.1.RELEASE编写,包括新版本的特性都会一块儿介绍...

说点什么

  • 我的QQ:1837307557

  • battcn开源群(适合新手):391619659

  • 微信公众号(欢迎调戏): battcn

我的博客:http://blog.battcn.com/

全文代码:https://github.com/battcn/spring-boot2-learning/tree/master/chapter2


本文分享自微信公众号 - battcn(battcn)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索