springboot多环境配置(spring.profiles.active)
springboot多环境配置前言一开始学习springboot主要以功能实现为主,有的细节并没有特别的注意,比如aop实现的区别,多环境方式部署怎么去切换环境等,其中多环境的配置就是很实用的一种配置复习使用配置相关的注解@Value@ConfigurationProperties(prefix = “属性名的前缀”)+@Component说明我之前整理了关于springboot读取配置文件相关的
springboot多环境配置
前言
一开始学习springboot主要以功能实现为主,有的细节并没有特别的注意,比如aop实现的区别,多环境方式部署怎么去切换环境等,其中多环境的配置就是很实用的一种配置
复习使用配置相关的注解
@Value
@ConfigurationProperties(prefix = “属性名的前缀”)+@Component
说明
我之前整理了关于springboot读取配置文件相关的注解和配置
参考网址:
https://blog.csdn.net/shaoming314/article/details/108569434
这次回顾的时候的踩坑记录
# @ConfigurationProperties注解使用无效
# 注意检查再使用被该类注解的类是要是用注入,而不是new
# 说明:
# 一般使用springboot配置和容器相关的东西不能使用new
# 要进行注入
1.新建springboot工程
说明:
测试期间,我们只需要导入web依赖
2.配置springboot配置(重要)
此demo使用的是properties,yml文件同理
application.properties
spring.profiles.active=dev
project.name=springboot-first
application-dev.properties
server.port=8081
my.environment=dev
user.username=dev-root
user.no=8081
application-dev.properties
server.port=8083
my.environment=prod
user.username=dev-prod
user.no=8083
application-test.properties
server.port=8082
my.environment=test
user.username=dev-test
user.no=8082
3.测试的配置类
package com.shaoming.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Auther: shaoming
* @Date: 2020/12/22 16:37
* @Description:
*
@ConfigurationProperties注解使用无效
注意检查再使用被该类注解的类是要是用注入,而不是new
说明:
一般使用springboot配置和容器相关的东西不能使用new
要进行注入
*/
@Data
@Component
@ConfigurationProperties(prefix = "user")
public class UserConfig {
private String username;
private Integer no;
}
4.测试的controller
package com.shaoming.controller;
import com.shaoming.config.UserConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @Auther: shaoming
* @Date: 2020/12/18 10:28
* @Description:
*/
@RestController
@Slf4j
public class HelloController {
@Value("${project.name}")
private String projectName;
@Value("${server.port}")
private String port;
@Value("${my.environment}")
private String environment;
@Autowired
private UserConfig userConfig;
@GetMapping("/hello")//get请求
public String gethello() {
/**
* @ConfigurationProperties注解使用无效
* 注意检查再使用被该类注解的类是要是用注入,而不是new
* 说明:
* 一般使用springboot配置和容器相关的东西不能使用new
* 要进行注入
*/
// UserConfig userConfig = new UserConfig();
log.info("user的具体信息为:[{}]",userConfig);
return "项目启动成功<hr>" +
"项目的端口号为" + port + "<hr>"
+ "项目的环境是" + environment + "<hr>"
+ "user的username的值为:" + this.userConfig.getUsername() + "<hr>" +
"user的no的值为: " + this.userConfig.getNo()+"<hr>" +
"项目名称: "+projectName;
}
}
5.打包部署的方式
打包后的名称:springboot-first-0.0.1-SNAPSHOT.jar
部署方式1
java -jar springboot-first-0.0.1-SNAPSHOT.jar
说明:
因为application.properties中配置是dev,所以启动项目使用的是dev配置文件的参数
部署方式2
java -jar springboot-first-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
说明:
当前方式启动使用的是application-test.properties的配置
6.pom.xml配置打包部署方式
pom.xml
<?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 https://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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shaoming</groupId>
<artifactId>springboot-first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-first</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--配置文件处理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- 配置环境 -->
<profiles>
<profile>
<!-- 开发 -->
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<!-- 测试 -->
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<activatedProperties>test</activatedProperties>
</properties>
</profile>
<profile>
<!-- 生产 -->
<id>prod</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
</project>
说明:
在配置部署环境
7.总结
5和6的方式都是可以的,推荐使用5这种打包部署的方式
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)