Spring Boot的Profile配置详解

2025-05-29 0 85

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,并配置相关内容信息

Spring Boot的Profile配置详解

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…

Spring Boot的Profile配置详解

可以看到配置信息就是dev的信息,可以切换成spring.profiles.active=prod测试看看。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://dtbuluo.com/125.html?utm_source=tuicool&utm_medium=referral

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring Boot的Profile配置详解 https://www.kuaiidc.com/116415.html

相关文章

发表评论
暂无评论