resources下application.yml、application-dev.yml、application-prod.yml多个配置文件。
spring: profiles: active: dev
spring: profiles: active: prod
一般都是通过修改spring.profiles.active值来修改加载不同环境的配置信息,可以把切换的dev/prod放到pom.xml文件来实现,避免手动修改。
1. 示例代码结构
2. pom文件
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.7.0</version> </parent> <groupid>vip.buddha</groupid> <artifactid>springboot-demo</artifactid> <version>1.0-snapshot</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <profiles> <!-- 开发环境 --> <profile> <id>dev</id> <activation> <activebydefault>true</activebydefault> </activation> <properties> <package.environment>dev</package.environment> </properties> </profile> <!-- 生产环境 --> <profile> <id>prod</id> <activation> <activebydefault>false</activebydefault> </activation> <properties> <package.environment>prod</package.environment> </properties> </profile> </profiles> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <!-- 添加以下配置,明确包含yaml文件 --> <includes> <include>**/*.yml</include> </includes> </resource> </resources> <plugins> <plugin> <artifactid>maven-resources-plugin</artifactid> <configuration> <delimiters> <delimiter>${}</delimiter> <!-- 使用 ${} 作为占位符 --> </delimiters> <usedefaultdelimiters>false</usedefaultdelimiters> <!-- 禁用默认的 @..@ --> </configuration> </plugin> </plugins> </build> </project>
3. application文件
spring: profiles: active: ${package.environment}
4. application-dev文件
spring: datasource: url: jdbc:mysql://localhost:3306/dev_db username: dev_user password: dev_password
5. application-prod文件
spring: datasource: url: jdbc:mysql://localhost:3306/prod_db username: prod_user password: prod_password
6. testcontroller文件
package vip.buddha.controller; import org.springframework.beans.factory.annotation.value; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class testcontroller { @value("${spring.datasource.url}") private string url; @value("${spring.datasource.username}") private string username; @value("${spring.datasource.password}") private string password; @requestmapping("/test") public void test() { system.out.println("url:" + url); system.out.println("username:" + username); system.out.println("password:" + password); } }
7. main文件
package vip.buddha; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class main { public static void main(string[] args) { springapplication.run(main.class, args); } }
8. 效果演示
maven面板中,先clean,后选择profiles为dev还是prod,再install后启动main主程序。浏览器访问http://localhost:8080/test,接口控制台就展示出预期结果来。
到此这篇关于springboot实现多环境配置文件切换的文章就介绍到这了,更多相关springboot配置文件切换内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论