SpringBoot——多环境配置文件、自定义配置文件的创建

简介: SpringBoot——多环境配置文件、自定义配置文件的创建

文章目录:


1.多环境配置文件的创建

1.1 dev

1.2 product

1.3 ready

1.4 test

1.5 核心配置文件

1.6 控制器类

1.7 入口类

2.自定义配置文件的创建

2.1 @Value

2.2 @ConfigurationProperties

2.2.1 自定义配置文件

2.2.2 自定义一个类,获取自定义配置文件中的属性值

2.2.3 控制器类

2.2.4 入口类

1.多环境配置文件的创建


在实际开发的过程中,我们的项目会经历很多的阶段(开发->测试->上线),每个阶段的配置也会不同,例如:端口、上下文根、数据库等,那么这个时候为了方便在不同的环境之间切换,SpringBoot 提供了多环境配置,具体步骤如下:👇👇👇

1.1 dev

  #开发环境配置文件
  server.port=8080
  server.servlet.context-path=/dev

1.2 product

  #生产环境配置文件
  server.port=8083
  server.servlet.context-path=/product

1.3 ready

  #准生产环境配置文件
  server.port=8082
  server.servlet.context-path=/ready

1.4 test

  #测试环境配置文件
  server.port=8081
  server.servlet.context-path=/test

1.5 核心配置文件

  #springboot核心配置文件
  spring.profiles.active=test

1.6 控制器类

package com.songzihao.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 *
 */
@Controller
public class IndexController {
    @RequestMapping(value = "/say")
    @ResponseBody
    public String say() {
        return "Hello springboot multi-environments";
    }
}

1.7 入口类

package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

springboot核心配置文件中,使用 spring.profiles.active=XXX,就可以指定其中的某一个配置文件。

spring.profiles.active=dev 👇👇👇

spring.profiles.active=test 👇👇👇

2.自定义配置文件的创建


SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配置,然后采用如下注解去读取配置的属性值。


2.1 @Value

在核心配置文件 applicatin.properties中,添加两个自定义配置项 school.name website。在 IDEA 中可以看到这两个属性不能被 SpringBoot 识别,背景是桔色的 

在核心配置文件 applicatin.yml 中,添加两个自定义配置项 school.name website


测试代码如下:👇👇👇

2.2 @ConfigurationProperties

2.2.1 自定义配置文件

#设置内嵌Tomcat端口号
server.port=8080
#设置上下文根
server.servlet.context-path=/
first.name=tencent
first.website=https://www.tencent.com
second.name=baidu
second.website=https://www.baidu.com

2.2.2 自定义一个类,获取自定义配置文件中的属性值

@ConfigurationProperties(prefix = "first") 这个注解,相当于获取到属性的前缀名,first.namefirst.website

package com.songzihao.springboot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 *
 */
@Component
@ConfigurationProperties(prefix = "first")
public class First {
    private String name;
    private String website;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getWebsite() {
        return website;
    }
    public void setWebsite(String website) {
        this.website = website;
    }
}

2.2.3 控制器类

package com.songzihao.springboot.controller;
import com.songzihao.springboot.config.First;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 *
 */
@Controller
public class IndexController {
    @Autowired
    private First first;
    @RequestMapping(value = "/say")
    @ResponseBody
    public String say() {
        return "first.name===" + first.getName() + " , first.website===" + first.getWebsite();
    }
}

2.2.4 入口类

package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

相关文章
|
24天前
|
Java 调度 Spring
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
44 0
|
3月前
|
Java
SpringBoot之文件上传(单文件与多文件上传的使用)
SpringBoot之文件上传(单文件与多文件上传的使用)
|
2月前
|
JavaScript 前端开发 Java
springboot整合minio+vue实现大文件分片上传,断点续传(复制可用,包含minio工具类)
springboot整合minio+vue实现大文件分片上传,断点续传(复制可用,包含minio工具类)
439 0
|
1月前
|
SQL Java 数据库连接
springboot解析txt文件顺便加到数据库中(nohup文件)
springboot解析txt文件顺便加到数据库中(nohup文件)
112 1
|
1月前
|
存储 JavaScript 前端开发
Spring Boot + Vue: 实现文件导入导出功能
本文介绍了使用Spring Boot和Vue实现文件导入导出的步骤。在后端,Spring Boot通过`MultipartFile`接收上传文件,保存至服务器,并使用`ResponseEntity`提供文件下载。前端部分,Vue项目借助`axios`发送HTTP请求,实现文件选择、上传及下载功能。这种前后端分离的实现方式提高了应用的可维护性和可扩展性。
38 2
|
1月前
|
Java Nacos 数据安全/隐私保护
springboot使用configtree读取树形文件目录中的配置
springboot使用configtree读取树形文件目录中的配置
springboot使用configtree读取树形文件目录中的配置
|
1月前
|
Java Spring
springboot项目读取 resources 目录下的文件的9种方式(总结)
springboot项目读取 resources 目录下的文件的9种方式(总结)
192 1
|
2月前
|
Java Docker 容器
docker部署springboot指定yml文件
docker部署springboot指定yml文件
57 0
|
2月前
|
druid JavaScript Java
SpringBoot+Vue.js实现大文件分片上传、断点续传与极速秒传
SpringBoot+Vue.js实现大文件分片上传、断点续传与极速秒传
106 0
QGS
|
3月前
|
JSON Java 关系型数据库
手拉手Springboot获取yml配置文件信息
手拉手Springboot获取yml配置文件信息
QGS
34 1