简体   繁体   English

如何使用@Value 访问 Spring 中的.properties 文件?

[英]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.

相关问题 如何使用Spring注释将Properties文件中的值注入到现有实例(不受Spring管理)的字段中? - How do I inject a value from Properties file to a field of an existing instance (not managed by Spring) using Spring annotations? 如何从Spring Boot War应用程序访问gradle.properties文件值? - How can I access a gradle.properties file value from a Spring Boot War application? 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot 如何使用Spring将嵌套键值对从属性文件加载到Java对象中? - How do I load nested key value pairs from a properties file into a Java object using Spring? 如何在Spring配置文件中使用可选属性文件? - How do I use an optional properties file in a Spring configuration file? 如何为spring boot创建内部属性文件? - How do I create a internal properties file for spring boot? 如何使用 spring boot 访问属性@value - How to access properties @value using spring boot Java:如何访问我的属性文件? - Java: How do I access my properties file? 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 如何使用主要方法访问Spring Boot应用程序中application.properties文件中的值 - How to access a value in the application.properties file in Spring Boot app with main method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM