【问题标题】:Spring boot application.properties doesn't work with multiple profilesSpring boot application.properties 不适用于多个配置文件
【发布时间】:2020-07-19 15:32:24
【问题描述】:

我在尝试连接到 aws 中的 dynamodb 时遇到 2 个问题。它在本地工作:

@Configuration
class DynamoDbConfig {
    @Value("${amazon.access.key}")
    private String awsAccessKey;

    @Value("${amazon.access.secret.key}")
    private String awsSecretKey;

    @Value("${amazon.dynamodb.endpoint}")
    private String awsDynamoDBEndPoint;

    @Value("${amazon.dynamodb.region}")
    private String awsDynamoDBRegion;

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    }

    public AWSCredentialsProvider amazonAWSCredentialsProvider() {
        return new AWSStaticCredentialsProvider(amazonAWSCredentials());
    }

    @Bean
    public DynamoDB dynamoDB() {
        AmazonDynamoDB amazonDynamoDB =  AmazonDynamoDBClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(awsDynamoDBEndPoint, awsDynamoDBRegion))
                .withCredentials(amazonAWSCredentialsProvider())
                .build();

        return new DynamoDB(amazonDynamoDB);
    }

}

应用程序本地属性

amazon.access.key=key1
amazon.access.secret.key=key2
amazon.dynamodb.endpoint=http://localhost:8000
amazon.dynamodb.region=us-east-1

应用程序-prod.properties

amazon.access.key=${AWS_ACCESS_KEY_ID}
amazon.access.secret.key=${AWS_SECRET_ACCESS_KEY}
amazon.dynamodb.endpoint=dynamodb.us-east-1.amazonaws.com
amazon.dynamodb.region=${AWS_DEFAULT_REGION}

我已经获得了凭据,并且我的 .aws/credentials 看起来不错:

[default]
aws_access_key_id = MyKeyId
aws_secret_access_key = MySecretKey
aws_session_token = blablabla
disney_session_expiration = This is also ok

1 问题)看起来总是使用 application-local.properties 配置文件,如果我在 DynamoDbConfig 类中显示 awsAccessKey 和 awsSecretKey,我会得到 key1 和 key2。我试过这两个命令:

mvn spring-boot:run -Dspring.profiles.active=prod
mvn spring-boot:run -Pprod

2 问题)我将 application-prod.properties 重命名为 application.properties 以使 spring 获取该配置文件,然后我收到以下错误消息:

Could not resolve placeholder 'AWS_SECRET_ACCESS_KEY' in value "${AWS_SECRET_ACCESS_KEY}"

【问题讨论】:

  • 如果您使用的是 Spring Boot 2+,请尝试 mvn spring-boot:run -Drun.profiles=prod
  • 您还可以在application.properties 文件中设置活动配置文件,使用spring.profiles.active=prod
  • 我使用的是 2.3.1,但你提供给我的命令不起作用
  • 我尝试在 application.properties 中使用 spring.profiles.active=local 并工作。但我想通过命令行激活配置文件,因为我必须部署它,每次都更改 application.properties 是不好的。也仍然无法解决问题 2
  • 也依赖于maven构建插件,试试mvn spring-boot:run -Dspring-boot.run.profiles=prod & mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=prod"

标签: spring-boot configuration


【解决方案1】:

我猜profile 不是问题,没有为以下键设置/定义值

${AWS_ACCESS_KEY_ID}
${AWS_SECRET_ACCESS_KEY}
${AWS_DEFAULT_REGION}

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 2020-05-05
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    相关资源
    最近更新 更多