如何读取 application.properties 并在 Spring 批处理中设置为作业参数

How to read from application.properties and set as Job Parameter in Spring Batch

我想从 application.properties 中读取 Spring Batch 应用程序的输入和输出路径,并将它们设置到 jobParametersBuilder,这样我就可以在整个作业执行过程中访问它们(以将它们分配给读者和作者)。

我能够在其他配置 classes 中读取 application.properties,但我似乎无法在我的主 class 中实现它。我需要在此处执行此操作,以便能够在执行作业之前将值分配给作业参数。

我的主要Class:

@SpringBootApplication
public class GleBatchApplication {


 private static final Logger logger = 
 LogManager.getLogger(FormateadorJobConfig.class);

    @Value("${file.input}")
    private static String inputPath;

    @Value("${file.output}")
    private static String outputPath;


public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {


     ApplicationContext ctx = SpringApplication.run(GleBatchApplication.class, args);

     JobLauncher lanzadorJob = ctx.getBean(JobLauncher.class);
     Job jobFormateador = ctx.getBean("jobFormateador", Job.class);
     JobParameters jobParameters = new JobParametersBuilder().
             addLong("Time in miliseconds: ", System.currentTimeMillis())
             .addString("inputPath", inputPath)
             .addString("outputPath", outputPath)
             .toJobParameters();

        System.out.println("Valor leido del properties: " + inputPath);
        System.out.println("Valor leido del properties: " + outputPath);


     JobExecution jobExecution = lanzadorJob.run(jobFormateador, jobParameters);
     logger.info("=================================================");
     logger.info("START TIME: " + jobExecution.getCreateTime());
     logger.info("FINISH TIME: " + jobExecution.getEndTime());
     logger.info("=================================================");

我的application.properties文件:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url = 
jdbc:mysql://localhost:3306/curso_batch_multiplefilewriting_2? 
autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.platform=mysql
spring.datasource.continueOnError=false

spring.batch.job.enabled=false


file.input = /inputFiles/GLEO-MN170100-PROCESO01-SUBDFACT-000001.txt
file.output = outputFiles/GLEO-MN1701-PROCESO001-SUBDFACT-FORMATDO-000001.txt

我也尝试过为输入和输出做一个单独的配置 class,但我不明白如何从我的主 class 调用它:

输入输出配置:

@Configuration
@PropertySource("classpath:application.properties")
public class InOutConfiguration {

@Value("${file.input}")
 private  String inputPath;

@Value("${file.output}")
 private  String outputPath;


@Bean
  public static PropertySourcesPlaceholderConfigurer   propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
  }

public  String getInputPath() {
    return inputPath;
}

public  void setInputPath(String inputPath) {
    this.inputPath = inputPath;
}

public  String getOutputPath() {
    return outputPath;
}

public  void setOutputPath(String outputPath) {
    this.outputPath = outputPath;
}

}

我正在获取 inputPath = null 和 outputPath = null。

下面的代码有效(我相信你的 application.properties 文件在 src/main/resources 中):

            Properties properties = new Properties();
            InputStream file = null;
            try {
                file = GleBatchApplication.class.getClassLoader().getResourceAsStream("application.properties");
                properties.load(file);
            }catch(Exception e) {
                //exception handling
            }

将它放在您的主要方法中,您可以从 "properties" 变量中读取值。喜欢:properties.getProperty("spring.datasource.driverClassName");

或者您可以将上述代码放在不同的class中,然后调用该方法来获取属性。这样您就可以随时随地使用它。