Spring 没有标准的带@ConfigurationProperties 的引导应用程序application.properties

Spring Boot application with @ConfigurationProperties without standard application.properties

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

@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 引导中将属性映射到属性 class 而不使用 Spring 引导中的标准 属性 文件命名约定的任何见解将不胜感激。

这是属性文件:

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

注意:我稍微修改了配置 class,现在出现以下异常:

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]

在类路径中添加 属性 文件或将其保存在资源文件夹中,然后按以下方式更新 @PropertySource

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

@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这里