Spring Boot 2.X 学习日记——配置管理


上一节中,我们用 Spring Boot实现了一个简单的Web应用,但如果将这个项目作为生产级的项目,显然是不足够的。接下来,在本节中,笔者将带着大家一起来学习,如何在 Spring Boot中添加一些配置信息。

自定义配置

Spring Boot针对添加配置信息这块,一共提供了2种方案。

配置文件

  1. 在./resources中,新增application.yml(application.properties),添加如下配置

    my :
      name : heheda
      message : Hello
    
  2. 新增主函数

    package com.boot.demo;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class BootApplication {
    
        @Value("${my.name}")
        private String name;
    
        @Value("${my.name}")
        private String message;
    
        public static void main(String[] args) {
            SpringApplication.run(BootApplication.class,args);
        }
    
        @GetMapping("/")
        public String hello(){
            return String.format("Hello %s,this is your message : %s",name,message);
        }
    }
    
  3. 运行主函数,访问http://localhost:8080/,结果如下

    在这里插入图片描述

命令行配置

  1. 点击Edit Configuration

    在这里插入图片描述

  2. Program arguments中添加如下启动参数

    --my.message="commend line init"
    
  3. 运行主函数,访问http://localhost:8080/,结果如下

    在这里插入图片描述

    tips:从以上两个例子中,我们可以知道命令行中添加的配置是优先于配置文件中的。

@ConfigurationProperties

@Value这个注解可以很方便的将我们所需的配置信息给注入进正确位置,但是假如我们存在很多配置信息需要注入时,如果用**@Value注入的话,代码看上去就会非常恶心。优秀的Spring Boot**框架给我们提供了另一种配置注入的方法,可以让我们避免看到以上这种恶心的代码。

  1. 添加如下依赖

    compileOnly "org.springframework.boot:spring-boot-configuration-processor"
    
  2. 在com.boot.demo.config.properties下新增MyProperties类

    package com.boot.demo.config.properties;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties("my")
    public class MyProperties {
        private String name;
    
        private String message;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
  3. 修改BootApplication

    package com.boot.demo;
    
    import com.boot.demo.config.properties.MyProperties;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class BootApplication {
      
        @Autowired
        private MyProperties myProperties;
    
        public static void main(String[] args) {
            SpringApplication.run(BootApplication.class,args);
        }
    
        @GetMapping("/")
        public String hello(){
            return String.format("Hello %s,this is your message : %s",myProperties.getName(),myProperties.getMessage());
        }
    }
    
  4. 运行主函数,访问http://localhost:8080/,结果如下

    在这里插入图片描述

多环境配置

在实际开发中,我们往往会根据环境的不同,选择不同的配置属性。以前在SpringMVC的项目中,我们用的是profile这个属性来选择加载不同的配置文件,同理,我们在Spring Boot中,也是用这个属性来选择加载不同的配置。

  1. 在./resources目录下新增application-dev.yml

    server:
      servlet:
        context-path: '/dev'
    
  2. 在application.yml中,添加如下配置

    spring:
      profiles:
        active: dev
    
  3. 运行主函数,访问http://localhost:8080/,结果如下

    在这里插入图片描述

  4. 访问http://localhost:8080/dev/,结果如下

在这里插入图片描述

全文代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值