how to use Spring Boot profiles

前端 未结 12 1174
忘了有多久
忘了有多久 2020-12-04 21:14

i have application.yml,application-dev.ymlandapplication-dev.yml

  1. I\'m using the maven command mvn spring-boot:run
12条回答
  •  时光取名叫无心
    2020-12-04 22:05

    You don't need three .yml files for this. You can have a single application.yml file and write profile specific properties in the same where each profile section is separated by 3 hyphen (---)

    Next, for selecting the current active profile, you can specify that as well in your application.yml file, like this :

    spring:
      profiles:
        active:
        - local
    

    However, this configuration will be overriden if you set an Environment variable, eg : SPRING_PROFILES_ACTIVE = dev


    Here is a sample file for you requirement:

    # include common properties for every profile in this section
    
    server.port: 5000 
    
    spring:
      profiles:
        active:
        - local
    
    ---
    # profile specific properties
    
    spring:
      profiles: local
    
      datasource:
        url: jdbc:mysql://localhost:3306/
        username: root
        password: root
    
    ---
    # profile specific properties
    
    spring:
      profiles: dev
    
      datasource:
        url: jdbc:mysql://
        username: 
        password: 
    

提交回复
热议问题