我扩展了我的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);
}
我的application-dev.yml包括以下内容:
amazonProperties:
endpointUrl: https://s3.eu-central-1.amazonaws.com
accessKey: XYZ
secretKey: XYZ
bucketName: XYZ
当我用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}"
由于您使用的是jhipster,这是因为您将凭据放在application-dev.yml中,并且此文件仅在dev配置文件中可见
你需要在application.yml
文件夹下放入src/main/test/resources
文件。测试运行器将在此文件中查找属性。