SpringBoot——多环境配置文件、自定义配置文件的创建
  7PTocT0TRZH3 2023年11月02日 62 0


文章目录:

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 提供了多环境配置,具体步骤如下:👇👇👇

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

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  :👇👇👇

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

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

spring.profiles.active=test  :👇👇👇

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

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


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

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

2.1 @Value 

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

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

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

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

测试代码如下:👇👇👇

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

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.name、first.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);
    }

}

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

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
7PTocT0TRZH3