[英]How do i access .properties file in Spring with @Value?
I'm trying to get some info from my config.properties
file which contains all my credential information something like this below:我正在尝试从我的
config.properties
文件中获取一些信息,该文件包含我的所有凭据信息,如下所示:
cloud.aws.credentials.accessKey=Hello
cloud.aws.credentials.secretKey=Hellobebe
cloud.aws.s3.bucket=hellome
could.aws.region.static=ap-northeast-2
cloud.stack.auto=false
and in java file, I'm trying access it by using @Value but i can't get the value.在 java 文件中,我正在尝试使用 @Value 访问它,但我无法获得该值。 here's my code for S3Service.java
这是我的 S3Service.java 代码
package com.aplusinternational.goTrip.service;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import lombok.NoArgsConstructor;
@Service
@NoArgsConstructor
public class S3Service {
private AmazonS3 s3Client;
@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;
@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;
@Value("${cloud.aws.s3.bucket}")
private String bucket;
@Value("${cloud.aws.region.static}")
private String region;
@PostConstruct
public void setS3Client() {
AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(this.region)
.build();
}
public String upload(MultipartFile multipartFile, String dirName) throws IOException {
String fileName = dirName + "/" + multipartFile.getOriginalFilename();
String uploadImageUrl = putS3(multipartFile, fileName);
//removeNewFile(multipartFile);
return uploadImageUrl;
}
private String putS3(MultipartFile multipartFile, String fileName) throws IOException {
System.out.println("file is in!: "+ fileName+" putS3");
System.out.println(this.bucket);
s3Client.putObject(new PutObjectRequest(bucket, fileName, multipartFile.getInputStream(), null).withCannedAcl(CannedAccessControlList.PublicRead));
return s3Client.getUrl(bucket, fileName).toString();
}
}
and when i run it, it will give me the result of ${cloud.aws.s3.bucket}
itself.当我运行它时,它会给我
${cloud.aws.s3.bucket}
本身的结果。 error's would be: enter image description here错误是:在此处输入图像描述
i'm spending thousand of hours for this.我为此花费了数千小时。 Please help me!
请帮我!
ps.附言。 i was able to get db info as from the same.properties file.
我能够从 same.properties 文件中获取数据库信息。
@PropertySource(value = {"classpath:config.properties",}, encoding = "utf-8") try to add this annotation. @PropertySource(value = {"classpath:config.properties",}, encoding = "utf-8") 尝试添加这个注解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.