Setting the default active profile in Spring-boot

前端 未结 14 1747
感动是毒
感动是毒 2020-12-13 08:20

I want my default active profile to be production if -Dspring.profiles.active is not set.

I tried the following in my application.pro

相关标签:
14条回答
  • 2020-12-13 08:49

    If you are using AWS Lambda with SprintBoot, then you must declare the following under environment variables:

    key: JAVA_TOOL_OPTIONS & value: -Dspring.profiles.active=dev

    0 讨论(0)
  • 2020-12-13 08:52

    add --spring.profiles.active=production

    Example:

    java -jar file.jar --spring.profiles.active=production
    
    0 讨论(0)
  • 2020-12-13 08:56

    I do it this way

        System.setProperty("spring.profiles.default", "dev");
    

    in the very beginning of main(...)

    0 讨论(0)
  • 2020-12-13 08:57

    If you're using maven I would do something like this:

    Being production your default profile:

    <properties>
        <activeProfile>production</activeProfile>
    </properties>
    

    And as an example of other profiles:

    <profiles>
        <!--Your default profile... selected if none specified-->
        <profile>
            <id>production</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activeProfile>production</activeProfile>
            </properties>
        </profile>
    
        <!--Profile 2-->
        <profile>
            <id>development</id>
            <properties>
                <activeProfile>development</activeProfile>
            </properties>
        </profile>
    
        <!--Profile 3-->
        <profile>
            <id>otherprofile</id>
            <properties>
                <activeProfile>otherprofile</activeProfile>
            </properties>
        </profile>
    <profiles>
    

    In your application.properties you'll have to set:

    spring.profiles.active=@activeProfile@
    

    This works for me every time, hope it solves your problem.

    0 讨论(0)
  • 2020-12-13 08:59

    Try this: @PropertySource("classpath:${spring.profiles.active:production}_file.properties")

    0 讨论(0)
  • 2020-12-13 09:02

    In AWS LAMBDA:

    For $ sam local you add the following line in your sam template yml file:

    Resources:
       FunctionName:
           Properties:
               Environment:
                   Variables:
                      SPRING_PROFILES_ACTIVE: local
    

    But in AWS Console: in your Lambda Environment variables just add:

    KEY:JAVA_TOOL_OPTIONS VALUE:-Dspring.profiles.active=dev

    0 讨论(0)
提交回复
热议问题