Spring Boot (八):多环境配置

本文介绍了一种使用 Spring Boot 进行多环境配置的方法,通过创建不同的配置文件来区分开发、测试等环境,并展示了如何在代码中读取这些配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍
一般我们开发过程中,会根据不同的环境来配置不同的数据库。
一、配置文件
添加三个不同的配置文件,来区分开发,测试环境。
application.properties

#springboot多环境配置======begin
test.url=local

#指定哪个profile
#spring.profiles.active=dev
#springboot多环境配置======end

application-dev.properties

test.url=dev.com

application-test.properties

test.url=test.com

二、代码
启动类

package com.guo;

import org.springframework.boot.SpringApplication;
@SpringBootApplication
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

控制类

package com.guo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class ActiveController {
	
	@Value("${test.url}")
	private String domain;

	@GetMapping("url")
	public Object test(){
       return domain;
	}	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值