简体   繁体   中英

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:

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. here's my code for 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. 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.

@PropertySource(value = {"classpath:config.properties",}, encoding = "utf-8") try to add this annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM