spring boot多环境配置

本文详细介绍如何在SpringBoot项目中实现开发、测试和生产环境的独立配置,避免了打包时频繁修改配置文件的繁琐操作。通过使用application-{profile}

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

在我们的实际开发中,一般都有三套环境,开发环境,测试环境,生产环境,三套环境的数据库连接配置也有所不同,比如,端口,IP地址等等。如果在打包时候都频繁的修改配置文件信息,那必将是非常容易出错的地方。

  在springBoot多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识.

application-dev.properties

com.name=this is dev
com.gender=man

application-test.properties

com.name=this is test
com.gender=man

application-pro.properties

com.name=this is pro
com.gender=man

新建一个类来获取配置文件中的值

package com.hf.moredev.model;

import lombok.Data;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Date: 2019/2/14
 * @Auther:
 */
@Component
@Data
@ToString
public class User {
    @Value("${com.name}")
    private String name;
    @Value("${com.gender}")
    private String gender;
}

测试:

package com.hf.moredev.controller;

import com.hf.moredev.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Date: 2019/2/14
 * @Auther: 
 */
@RestController
public class TestController {
    @Autowired
    private User user;
    @GetMapping("/user")
    public String test(){
        System.out.println(user.getName());
        return user.toString();
    }
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值