Profile 是Spring Boot用来针对不同的环境对不同的配置提供的支持,全局Profile配置使用application-{profile}.properties,如: application-dev.properties 可以表示为开发环境。
然后通过application.properties文件中的spring.profiles.active=dev来设置
在src/main/resources下面新建 application-dev.properties和application-prod.properties,并配置相关内容信息
application-prod.properties内容为:
?
1
2
3
4
5
|
server.context-path=/product
server.port=8080
author.name=Product
author.age=25
|
application-dev.properties内容为:
?
1
2
3
4
5
|
server.context-path=/dev
server.port=9090
author.name=Dev
author.age=21
|
DemoApplication的代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@ SpringBootApplication(scanBasePackages = "com.example" )
@RestController
public class DemoApplication {
@Autowired
private Author author;
@RequestMapping ( "/" )
public String index() {
return "Hello " + author.getName() + ",Your age is " + author.getAge();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication. class , args);
}
}
|
其中 Author代码如下: @ConfigurationProperties用作加载配置资源, prefix前缀符
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Component
@ConfigurationProperties (prefix = "author" )
public class Author {
private String name;
private Long age;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this .age = age;
}
}
|
设置application.properties的内容:
?
1
|
spring.profiles.active=dev
|
表示dev环境,运行Spring Boot APP…
可以看到配置信息就是dev的信息,可以切换成spring.profiles.active=prod测试看看。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://dtbuluo.com/125.html?utm_source=tuicool&utm_medium=referral
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
Win10电脑关机太慢怎么办?Win10电脑快速关机方法教程
2025-05-27 49 -
2025-05-27 19
-
2025-05-27 25
-
2025-05-25 15
-
2025-05-29 48
热门评论