我正在编写一个从SQS读取消息的Spring Boot应用程序。我可以使用环境变量AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY运行该应用程序。但是,我想知道通过类似于application.properties的文件传递此配置会更简单。如何实现呢?

最佳答案

在Spring Boot应用程序中,您可以使用application.yml批注访问@value文件中提到的属性。您可以创建如下服务:

@Service
public class AmazonClient {
    private AmazonSQS sqsClient;

    @Value("${amazonProperties.accessKey}")
    private String accessKey;
    @Value("${amazonProperties.secretKey}")
    private String secretKey;

    @PostConstruct
    private void initializeAmazon() {
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
        this.sqsClient = AmazonSQSClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .build();

    }
}

application.yml文件中:
amazonProperties:
   accessKey: <your_access_key>
   secretKey: <your_secret_key>

10-04 19:18