简体   繁体   English

Spring未从属性文件加载AWS-SNS凭证

[英]Spring not loading AWS-SNS credentials from properties file

I am building an application that reads data from SQL Server databases and then will send SMS text messages based on certain data. 我正在构建一个应用程序,该应用程序从SQL Server数据库中读取数据,然后将基于某些数据发送SMS文本消息。

The application is in Java, with Spring and Maven. 该应用程序使用Java和Spring和Maven。

The problem I am encountering is my application.properties file is not being loaded in correctly. 我遇到的问题是我的application.properties文件未正确加载。 I get an "IllegalArguementException: Access Key cannot be null" when amazonSNS is created 创建amazonSNS时收到“ IllegalArguementException:访问密钥不能为空”

I have two properties files, one for dev one for prod 我有两个属性文件,一个用于开发,一个用于生产

I have tried using @PropertySource in my AppConfig class to specify which properties file to load in, I still get null for aws access value. 我尝试在我的AppConfig类中使用@PropertySource指定要加载到的属性文件,但仍然无法获得AWS访问值的null。 However i dont want to use @PropertySource because I have two profiles dev and prod 但是我不想使用@PropertySource,因为我有两个配置文件dev和prod

My application-dev.properties AWS credentials are as such 我的application-dev.properties AWS凭证就是这样

aws.sns.type=Transactional
aws.access=123456etc
aws.secret=123456etc 

AppConfig file AppConfig文件

@Configuration
@ComponentScan(basePackages = "com.application")
@EnableIntegration
@EnableScheduling
public class AppConfig implements EnvironmentAware {

    private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);

    private Environment environment;
    @Value("${aws.access}")
    private String awsAccessKey;
    @Value("${aws.secret}")
    private String awsSecretKey;

    @Bean
    public AmazonSNS amazonSNS() {
        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
        AmazonSNS snsClient = AmazonSNSClient.builder().withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)).build();
        logger.info("Creating snsClient");
        return snsClient;
    }

so to fix this I learned that @Value was not working for some reason. 因此,要解决此问题,我了解到@Value由于某种原因无法正常工作。 Intellij was telling me that it knew what that value was but when it was loaded it was null Intellij告诉我它知道那个值是什么,但是在加载时它为null

so instead of using @Value to get the aws.access and aws.secret I used this.environment.getProperty("aws.access") 所以我没有使用@Value来获取aws.access和aws.secret,而是使用了this.environment.getProperty(“ aws.access”)

like this 像这样

BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(this.environment.getProperty("aws.access"), this.environment.getProperty("aws.secret"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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