我扩展了我的 JHipster 应用程序并使用以下代码编写了一个服务类:
@Component
@Transactional
public class AmazonClient {
private AmazonS3 s3Client;
@Value("${amazonProperties.endpointUrl}")
private String endpointUrl;
@Value("${amazonProperties.bucketName}")
private String bucketName;
@Value("${amazonProperties.accessKey}")
private String accessKey;
@Value("${amazonProperties.secretKey}")
private String secretKey;
public AmazonClient() {
}
@PostConstruct
private void initializeAmazon() {
AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
this.s3Client = new AmazonS3Client(credentials);
}
Run Code Online (Sandbox Code Playgroud)
我的 application-dev.yml 包括以下内容:
amazonProperties:
endpointUrl: https://s3.eu-central-1.amazonaws.com
accessKey: XYZ
secretKey: XYZ
bucketName: XYZ
Run Code Online (Sandbox Code Playgroud)
当我用 mvwn 启动我的应用程序时,一切正常。当我运行我的测试时,我得到以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonClient': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'amazonProperties.endpointUrl' in value "${amazonProperties.endpointUrl}"
Run Code Online (Sandbox Code Playgroud)
由于您使用的是 jhipster,发生这种情况是因为您将凭据放在 application-dev.yml 中,并且此文件仅在开发配置文件中可见
您需要将application.yml文件放入文件src/main/test/resources夹下。测试运行器将在此文件中查找属性。
| 归档时间: |
|
| 查看次数: |
773 次 |
| 最近记录: |