一、使用Profile文件进行多环境配置
1、模拟开发环境,创建配置文件application-dev.properties![在这里插入图片描述](https://img-blog.csdnimg.cn/20210508145533361.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3MzQ5Mjg3,size_16,color_FFFFFF,t_70)
2、模拟测试环境,创建配置文件application-test.properties![在这里插入图片描述](https://img-blog.csdnimg.cn/20210507144143797.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3MzQ5Mjg3,size_16,color_FFFFFF,t_70)
3、模拟生产环境,创建配置文件application-pro.properties![在这里插入图片描述](https://img-blog.csdnimg.cn/20210507144214402.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3MzQ5Mjg3,size_16,color_FFFFFF,t_70)
4、指定使用环境
方式1 - - - 使用配置文件全局指定使用环境
-
在全局配置文件里指定当前使用环境 - 开发环境
启动项目:
-
在全局配置文件里指定当前使用环境 - 测试环境
启动项目:
-
在全局配置文件里指定当前使用环境 - 生产环境
启动项目:
-
还有通过命令行方式指定使用环境
二、使用@Profile注解进行多环境配置
(一)建数据库配置接口DatabaseConfig
package lesson5;
public interface DatabaseConfig {
void connector();
}
(二)创建三个数据库配置实现类
1、创建数据库配置实现类MySQLConfig
package lesson5;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration // 指定为配置类
@Profile("mysql") // 配置环境名称
public class MySQLConfig implements DatabaseConfig {
@Override
public void connector() {
System.out.println("项目使用MySQL数据库环境~");
}
}
2、创建数据库配置实现类OracleConfig
package lesson5;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration // 指定为配置类
@Profile("oracle") // 配置环境名称
public class OracleConfig implements DatabaseConfig {
@Override
public void connector() {
System.out.println("项目使用Oracle数据库环境~");
}
}
3、创建数据库配置实现类SybaseConfig
package lesson5;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration // 指定为配置类
@Profile("sybase") // 配置环境名称
public class SybaseConfig implements DatabaseConfig {
@Override
public void connector() {
System.out.println("项目使用Sybase数据库环境~");
}
}
(三)在全局配置文件里设置使用环境
- 配置使用环境
(四)打开测试类,编写测试方法
package lesson5;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Lesson5ApplicationTests {
// 注入数据库配置实体
@Autowired
private DatabaseConfig databaseConfig;
@Test
void contextLoads() {
// 调用数据库配置实体的方法
databaseConfig.connector();
}
}
- 运行
三、随机值设置以及参数间引用
1、全局配置随机参数
#任意一个随机数
my.number=${random.value}
#一个随机整数
my.integer=${random.int}
#一个长整型的随机数
my.long=${random.long}
#获取一个随机的UUID值
my.uuid=${random.uuid}
#小于10的随机整数
my.number.less=${random.int(10)}
#随机产生1024至65535之间的数
my.number.range=${random.int[1024,65535]}
- 测试随机数my.number
package demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
// 注入配置文件里的属性
@Value("${my.number}")
private String number;
@Test
void contextLoads() {
// 输出配置文件里的属性
System.out.println(number);
}
}
运行结果:
- 测试随机整数my.integer
运行结果:
2、参数间的引用