最代码官方的gravatar头像
最代码官方2020-12-20 11:31:56

Spring Boot配置@Profile注解加载不同环境的配置文件实例

项目描述

springboot启动时,可以通过参数

spring.profiles.active=prod

来加载不同的配置文件

也可以通过include参数指定加载其他的配置

spring:
    profiles:
        active:
            - default
        include: common

运行环境

jdk8+mysql+IntelliJ IDEA+maven

项目技术(必填)

spring boot

数据库文件(可选)

依赖包文件(可选)

maven搭建

链接:https://pan.baidu.com/s/14I37LqozknSqUXi_1jHpuw
提取码:qp4o

是否原创(转载必填原文地址)

转载https://memorynotfound.com/spring-boot-managing-profiles-profile-annotation-example/

项目截图(必填)

Spring Boot配置@Profile注解加载不同环境的配置文件实例

运行截图(必填)

Spring Boot配置@Profile注解加载不同环境的配置文件实例

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

2020-12-20 11:28:50.255  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Starting Application on CIFX5T2TRH4ZE3H with PID 1484 (started by Administrator in D:\workspace-idea\memorynotfound\spring-framework\spring-boot\profiles)
2020-12-20 11:28:50.260  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : The following profiles are active: common,dev
2020-12-20 11:28:50.390  INFO 1484 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]79e2c065: startup date [Sun Dec 20 11:28:50 CST 2020]; root of context hierarchy
2020-12-20 11:28:51.583  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Spring Boot - active profile: development profile
2020-12-20 11:28:51.583  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Spring Boot - Choosing Your Profile and @Profile annotation example
2020-12-20 11:28:51.583  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : ApplicationProperties{name='common-profile-name', email='[email protected]'}
2020-12-20 11:28:52.062  INFO 1484 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-12-20 11:28:52.130  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Started Application in 2.812 seconds (JVM running for 4.595)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

2020-12-20 11:28:52.373  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Starting Application on CIFX5T2TRH4ZE3H with PID 1484 (started by Administrator in D:\workspace-idea\memorynotfound\spring-framework\spring-boot\profiles)
2020-12-20 11:28:52.373  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : The following profiles are active: common,default
2020-12-20 11:28:52.385  INFO 1484 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]9ef8eb7: startup date [Sun Dec 20 11:28:52 CST 2020]; root of context hierarchy
2020-12-20 11:28:52.598  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Spring Boot - active profile: development profile
2020-12-20 11:28:52.598  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Spring Boot - Choosing Your Profile and @Profile annotation example
2020-12-20 11:28:52.598  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : ApplicationProperties{name='common-profile-name', email='[email protected]'}
2020-12-20 11:28:52.641  INFO 1484 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-12-20 11:28:52.646  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Started Application in 0.457 seconds (JVM running for 5.111)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

2020-12-20 11:28:52.770  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Starting Application on CIFX5T2TRH4ZE3H with PID 1484 (started by Administrator in D:\workspace-idea\memorynotfound\spring-framework\spring-boot\profiles)
2020-12-20 11:28:52.770  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : The following profiles are active: common,prod
2020-12-20 11:28:52.773  INFO 1484 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]75b25825: startup date [Sun Dec 20 11:28:52 CST 2020]; root of context hierarchy
2020-12-20 11:28:52.979  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Spring Boot - active profile: production profile
2020-12-20 11:28:52.979  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Spring Boot - Choosing Your Profile and @Profile annotation example
2020-12-20 11:28:52.979  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : ApplicationProperties{name='common-profile-name', email='[email protected]'}
2020-12-20 11:28:53.015  INFO 1484 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-12-20 11:28:53.019  INFO 1484 --- [           main] c.memorynotfound.springboot.Application  : Started Application in 0.361 seconds (JVM running for 5.484)
2020-12-20 11:28:53.024  INFO 1484 --- [       Thread-4] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]9ef8eb7: startup date [Sun Dec 20 11:28:52 CST 2020]; root of context hierarchy
2020-12-20 11:28:53.024  INFO 1484 --- [       Thread-6] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.Anno[email protected]: startup date [Sun Dec 20 11:28:52 CST 2020]; root of context hierarchy
2020-12-20 11:28:53.025  INFO 1484 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]79e2c065: startup date [Sun Dec 20 11:28:50 CST 2020]; root of context hierarchy
2020-12-20 11:28:53.029  INFO 1484 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2020-12-20 11:28:53.029  INFO 1484 --- [       Thread-6] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2020-12-20 11:28:53.028  INFO 1484 --- [       Thread-4] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Process finished with exit code 0

注意事项(可选)

可以研究下springboot加载配置文件的机制

https://cloud.tencent.com/developer/article/1497520


打赏

文件名:profiles.zip,文件大小:8.215K下载
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友