Springboot工程多环境配置

本文详细介绍了在Springboot项目中如何利用Profile进行多环境配置管理,包括通过Maven的profile配置和在application.yml中设置active属性两种方式,以及这两种方式的优缺点。

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

Profile

         随着系统的研发部署,工程会面临越来越多的不通环境的配置要求。最为常见的是dev(开发)、test(测试)、pro(生产)环境的区分,而profile文件的配置使得工程在面向不同环境时,可以灵活配置打包部署。

工程配置

通常profile配置有两种方式,以Springboot工程举例。

  • 在pom.xml文件中已经指定了配置文件目录,如:
    在这里插入图片描述
<bulid>
       <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/dev/**</exclude>
                    <exclude>**/pro/**</exclude>
                    <exclude>**/test/**</exclude>
                    <exclude>**/pre/**</exclude>
                </excludes>
            </resource>

            <resource>
                <directory>src/main/resources/${profile.path}</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
</build>
<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.path>config/dev</profile.path>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profile.path>config/pro</profile.path>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.path>config/test</profile.path>
            </properties>
        </profile>
        <profile>
            <id>pre</id>
            <properties>
                <profile.path>config/pre</profile.path>
            </properties>
        </profile>
    </profiles>

在maven打包时可以通过-P dev/test/pro 命令打出含有不同配置文件的jar包。

  • 除此之外,我们也可以通过application.yml中设置属性进行指定
spring:
    profiles:
        active: test

使用该方法时,上述pom.xml里面的配置就不需要了,但是需要将不同环境的系统配置文件重新按规则命名
application-{profile}.properties,如application-test.properties
但是值得注意的是,该方法在打出的包里包含所有环境的配置文件,这样安全性有所欠缺,下为jar包内目录:
在这里插入图片描述
示例文件:
application.yml

server:
    port: 52001
    context-path: /ldemo_device
    tomcat:
        accesslog: 
            enabled: true 
            directory: logs
            pattern: common
            prefix: access_lucas_device
        basedir: .
        maxConnections: -1
        maxThreads: 1000
        maxHttpPostSize: -1
        acceptCount: -1
spring:
    profiles:
        active: test

applicaction-dev.yml

spring:
    #datasource
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://xxx.xxx.xx.xx:3306/device?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
        username: dev
        password: dev
        driver-class-name: com.mysql.jdbc.Driver
        druid:
            initialSize: 5
            minIdle: 500
            maxActive: 1000
            maxWait: 60000
            timeBetweenEvictionRunsMillis: 60000
            minEvictableIdleTimeMillis: 300000
            validationQuery: SELECT 1
            testWhileIdle: true
            testOnBorrow: false
            testOnReturn: false

applicaction-test.yml

spring:
    #datasource
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://xxx.xxx.xx.xx:3306/device?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
        username: test
        password: test
        driver-class-name: com.mysql.jdbc.Driver
        druid:
            initialSize: 5
            minIdle: 500
            maxActive: 1000
            maxWait: 60000
            timeBetweenEvictionRunsMillis: 60000
            minEvictableIdleTimeMillis: 300000
            validationQuery: SELECT 1
            testWhileIdle: true
            testOnBorrow: false
            testOnReturn: false

          如遇到Springboot工程启动时打印日志No active profile set, falling back to default profiles: default,该日志级别是INFO,并不会对系统运行产生影响。产生该日志的原因是spring容器未找到系统profile配置。即使使用pom配置的方法,工程内没有application-{profile}.yml文件,但在application.yml中指定profiles:dev也不会对工程产生影响,启动日志会打印
在这里插入图片描述

### 使用 Spring Boot 搭建项目框架及配置运行环境 #### 1. 环境准备 为了成功搭建并运行一个基于Spring Boot的应用程序,需要确保已安装JDK版本为1.8及以上,并且已经正确设置了JAVA_HOME环境变量[^2]。 对于构建工具的选择,在此推荐使用Maven作为依赖管理工具。同样地,Eclipse IDE加上STS(Spring Tool Suite)插件能够极大地简化开发流程,提供更友好的界面和支持功能。 #### 2. 创建新的Spring Boot应用程序 可以通过多种方式创建一个新的Spring Boot工程: - **通过Spring Initializr在线服务**:访问[start.spring.io](https://start.spring.io/)网站,按照提示填写相关信息(如Group、Artifact ID等),选择所需的依赖项之后下载生成的ZIP文件解压即可得到完整的项目结构。 - **利用IDE内置向导**:如果已经在Eclipse中安装了STS,则可以直接在新建项目时选择“Spring Starter Project”,然后按需定制化设置完成初始化工作。 无论采用哪种方法建立新项目,都应确保`pom.xml`文件内包含了必要的启动器依赖,比如web支持相关的starter-web组件,这有助于自动装配相应的Bean实例[^1]。 ```xml <dependencies> <!-- Web support --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Other dependencies... --> </dependencies> ``` #### 3. 启用自动化配置 为了让Spring Boot根据类路径下的jar包来自动生成合理的默认配置方案,只需简单地标记主类上加注解@EnableAutoConfiguration即可。实际上,这是Spring Boot的核心特性之一,它能依据classpath中存在的库来推断出最合适的配置选项。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // This annotation is equivalent to using @EnableAutoConfiguration and others. public class MyApplication { public static void main(String[] args){ SpringApplication.run(MyApplication.class,args); } } ``` #### 4. 配置热部署以提升开发效率 为了加快迭代速度,可以在项目的`pom.xml`里加入spring-boot-devtools依赖,从而开启热部署的功能。当源码发生变动后,无需重新启动整个应用就能立即看到效果更新[^3]。 ```xml <!-- Hot deployment --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> ``` 同时还需要调整application.properties或yml格式的全局属性文件,指定哪些资源应该被排除在外不参与实时监控刷新操作,防止不必要的频繁重启行为影响性能表现。 ```properties # Enable hot deployment spring.devtools.restart.enabled=true # Exclude certain directories from being monitored for changes that trigger restarts spring.devtools.restart.exclude=static/** ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值