【问题标题】:Spring Boot application with @ConfigurationProperties without standard application.properties带有 @ConfigurationProperties 的 Spring Boot 应用程序没有标准的 application.properties
【发布时间】:2018-03-21 03:20:57
【问题描述】:

我有一个 Spring Boot 应用程序,它需要使用不同于标准 application.properties 的文件中的属性。我想将我的属性文件映射到特定的属性类,并能够在配置类中使用属​​性类。代码如下:

@Configuration
@EnableConfigurationProperties(AWSProperties.class)
@PropertySource("kaizen-batch-common-aws.properties")
public class ResourceConfigAWS {

    @Autowired
    private AWSProperties awsProperties;

    @Autowired
    private ResourceLoader resourceLoader;

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

    @Bean
    public AmazonS3 amazonS3Client() {
        logger.debug("AWS Config: " + this.awsProperties);
        BasicAWSCredentials awsCreds = new BasicAWSCredentials("access_key_id", "secret_key_id");
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .withRegion(awsProperties.getRegion())
                .build();
        return s3Client;
    }

    @Bean
    public SimpleStorageResourceLoader resourceLoader() {
        return new SimpleStorageResourceLoader(this.amazonS3Client());
    }

    @Bean
    @StepScope
    public Resource getResourceToProcess(@Value("#{jobParameters[T(com.kaizen.batch.common.JobRunnerTemplate).INPUT_FILE_PARAM_NAME]}") String inputFile) {
        return this.resourceLoader.getResource(this.awsProperties.getInputLocation() + inputFile);
    }

    @PostConstruct
    public void postConstruct() {
        System.out.print("Properties values: " + this.awsProperties);
    }

    @Bean
    public AbstractFileValidator inputFileValidator() {
        InputS3Validator inputS3Validator = new InputS3Validator();
        inputS3Validator.setRequiredKeys(new String[]{InputFileSystemValidator.INPUT_FILE});
        return inputS3Validator;
    }

    @Bean
    public InputFileFinalizerDelegate inputFileFinalizerDelegate() {
        InputFileFinalizerDelegate inputFileFinalizerDelegate = new AWSInputFileFinalizerDelegate();
        return inputFileFinalizerDelegate;
    }

    @Bean
    public InputFileInitializerDelegate inputFileInitializerDelegate() {
        InputFileInitializerDelegate inputFileInitializerDelegate = new AWSInputFileInitializerDelegate();
        return inputFileInitializerDelegate;
    }


}


@ConfigurationProperties("aws")
@Data
public class AWSProperties {

    private static final String SEPARATOR = "/";

    private static final String S3_PREFFIX = "s3://";

    @Value("${s3.bucket.batch}")
    private String bucket;

    @Value("${s3.bucket.batch.batch-job-folder}")
    private String rootFolder;

    @Value("${s3.bucket.batch.input}")
    private String inputFolder;

    @Value("${s3.bucket.batch.processed}")
    private String processedFolder;

    @Value("${region}")
    private String region;

    public String getInputLocation() {
        return this.getBasePath() + this.inputFolder + SEPARATOR;
    }

    public String getProcessedLocation() {
        return this.getBasePath() + this.processedFolder + SEPARATOR;
    }

    private String getBasePath() {
        return S3_PREFFIX + this.bucket + SEPARATOR + this.rootFolder + SEPARATOR;
    }

}

我正在努力设法获取 AWSProperties 以填充属性文件中定义的值,不知何故,我总是以空值结束 awsProperties。任何有关如何在不使用 Spring Boot 中的标准属性文件命名约定的情况下将 Spring Boot 中的属性映射到 Properties 类的见解将不胜感激。

这是属性文件:

aws.s3.bucket.batch=com-kaizen-batch-dev
aws.s3.bucket.batch.input=input
aws.s3.bucket.batch.processed=processed
aws.s3.bucket.batch.batch-job-folder=merchant-file
aws.region=us-east-2

注意:我稍微修改了配置类,现在我得到了以下异常:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.kaizen.batch.common.ResourceConfigAWS': Unsatisfied dependency expressed through field 'awsProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aws-com.kaizen.batch.common.AWSProperties': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 's3.bucket.batch' in value "${s3.bucket.batch}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]

【问题讨论】:

  • 您已使用 @ConfigurationProperties("aws") 注释 AWSProperties 类,同时所有可配置字段都使用 @Value 注释。您能否澄清您尝试使用@ConfigurationProperties 配置哪些字段?还包括要与 AWSProperties 类绑定的属性(键值对)。
  • 感谢 sanit,我添加了属性文件,我还修改了代码,现在我得到一个特定的异常,表明它无法绑定 AWSProperties 中的特定属性

标签: java spring spring-boot properties-file


【解决方案1】:

@ConfigurationProperties 用于将属性文件键与 POJO 类绑定。您必须进行以下更改才能使您的代码正常工作。

kaizen-batch-common-aws.properties

aws.s3.bucket.batch.bucket=com-kaizen-batch-dev
aws.s3.bucket.batch.root-folder=processed
aws.s3.bucket.batch.input-folder=input
aws.s3.bucket.batch.processed-folder=processed
aws.s3.bucket.batch.batch-job-folder=merchant-file


@Component
@ConfigurationProperties(prefix = "aws.s3.bucket.batch")
public class AWSProperties {
    private String bucket;
    private String rootFolder;
    private String inputFolder;
    private String processedFolder;
    private String region;

    // setter require
}

请在此处访问doc

【讨论】:

  • 非常感谢!这解决了我的问题!
【解决方案2】:

在类路径中添加属性文件或将其保存在资源文件夹中并以这种方式更新@PropertySource

@PropertySource("classpath:kaizen-batch-common-aws.properties")

【讨论】:

  • 谢谢,我试过了,可惜没用。
猜你喜欢
  • 2023-02-10
  • 2019-12-26
  • 1970-01-01
  • 2017-04-16
  • 2016-08-07
  • 2018-12-05
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多