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

时间:2018-03-21 03:20:57

标签: java spring spring-boot properties-file

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

@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

注意:我稍微修改了Configuration类,现在我收到以下异常:

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]

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

@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此处