具有不带标准应用程序属性的@ConfigurationProperties的 Spring Boot 应用程序



我有一个 Spring 引导应用程序,它要求在与标准 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 引导中的标准属性文件命名约定的情况下将 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

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

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用于

将属性文件键与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
}

请在此处访问文档

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

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

最新更新