Spring Boot学习5——多环境配置

本文详细介绍了SpringBoot如何进行多环境配置,包括使用Profile文件和@Profile注解的方式。通过创建不同环境的配置文件和接口实现,展示了如何根据环境切换数据库配置,并通过命令行指定环境启动项目。实操过程涵盖了从创建项目、配置文件、编写控制器到测试的全部步骤。
摘要由CSDN通过智能技术生成

为什么要使用多环境配置

在实际开发中,应用程序通常需要部署到不同的运行环境中,例如开发环境、测试环境、生产环境等。不同的环境可能需要不同的环境配置,针对这种情况,不可能手动变更配置文件来适应不同的开发环境,通常需要对项目进行多环境配置,Spring Boot框架提供了两种多环境配置的方式,分别是Profile文件多环境配置和@Profile注解多环境配置。

使用Profile文件进行多环境配置

一、创建项目ProfileDemo01

在这里插入图片描述
在这里插入图片描述

二、创建多环境配置文件

  • 将application.properties文件后缀改为yaml
    在这里插入图片描述
  • 配置文件命名格式:application-xxx.yaml,如果不是application则会被认定为自定义配置文件
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

三、创建控制器ProfileController

  • 创建controller子包
    在这里插入图片描述
package net.lj.lesson05.profiledemo01.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 项目资料控制器
 */
@RestController
public class ProfileController {
    @GetMapping("hello")
    public String hello(){
        return "Hello Spring Boot";
    }
}

使用全局配置文件指定使用环境

在这里插入图片描述

启动项目进行测试
  • 可以清楚看到配置的环境信息
    在这里插入图片描述
    在这里插入图片描述
    修改环境再次查看
    在这里插入图片描述

通过命令行方式

  • 使用IDEA将Maven项目打成jar包
    在这里插入图片描述
  • 在target目录会生成的项目jar包
    在这里插入图片描述
  • 在终端执行jar包,选择使用环境 - 开发环境
cd target
java -jar profiledemo01-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
  • 可以看到,使用环境不受全局配置文件的影响
    在这里插入图片描述
    Ctrl+C停止运行

使用@Profile注解进行多环境配置

  • @Profile注解适用于项目将用到多种数据库的环境

一、创建项目ProfileDemo02

在这里插入图片描述
在这里插入图片描述

二、创建数据库配置接口DatabaseConfig

  • 创建config子包
    在这里插入图片描述
package net.lj.lesson05.config;


/**
 * 数据库配置接口
 */
public interface DatabaseConfig {
    void connector();
}

三、创建数据库配置实现类MySQLConfig

在这里插入图片描述

package net.lj.lesson05.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * MySQL数据库配置类
 */
@Configuration // 指定为配置类
@Profile("mysql") // 指定配置环境名称
public class MySQLConfig implements DatabaseConfig {
    @Override
    public void connector() {
        System.out.println("项目使用MySQL数据库环境~");
    }
}

四、创建数据库配置实现类OracleConfig

在这里插入图片描述

package net.lj.lesson05.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * Oracle数据库配置类
 */
@Configuration // 指定为配置类
@Profile("oracle") // 指定配置环境名称
public class OracleConfig implements DatabaseConfig {
    @Override
    public void connector() {
        System.out.println("项目使用Oracle数据库环境~");
    }
}

五、创建数据库配置实现类SybaseConfig

在这里插入图片描述

package net.lj.lesson05.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * Sybase数据库配置类
 */
@Configuration // 指定为配置类
@Profile("sybase") // 指定配置环境名称
public class SybaseConfig implements DatabaseConfig {
    @Override
    public void connector() {
        System.out.println("项目使用Sybase数据库环境~");
    }
}

六、在全局配置文件application.properties里配置使用环境

在这里插入图片描述

在这里插入图片描述

七、编写测试方法

package net.lj.lesson05;

import net.lj.lesson05.config.DatabaseConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ProfileDemo02ApplicationTests {
    @Autowired
    private DatabaseConfig databaseConfig;

    @Test
    void contextLoads() {
        //调用数据库配置实体方法
        databaseConfig.connector();
    }

}

启动测试方法

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值