首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有标准application.properties的@ConfigurationProperties的Spring应用程序

没有标准application.properties的@ConfigurationProperties的Spring应用程序
EN

Stack Overflow用户
提问于 2018-03-21 03:20:57
回答 2查看 2.3K关注 0票数 0

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

代码语言:javascript
复制
@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中的属性映射到properties类而不使用Spring中的标准属性文件命名约定,我们将非常感激。

以下是属性文件:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-22 17:35:51

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

kaizen-batch-common-aws.properties

代码语言:javascript
复制
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
}

请访问这里的文档

票数 1
EN

Stack Overflow用户

发布于 2018-03-21 03:26:23

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

代码语言:javascript
复制
@PropertySource("classpath:kaizen-batch-common-aws.properties")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49397582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档