我想从我的 application.properties 中读取 Spring Batch 应用程序的输入和输出路径,并将它们设置为 jobParametersBuilder,这样我就可以在整个作业执行过程中访问它们(将它们分配给读取器和写入器)。
我能够从其他配置类中的 application.properties 中读取,但我似乎无法在我的主类中实现它。我需要在此处执行此操作,以便能够在执行作业之前将值分配给作业参数。
我的主要课程:
@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
我也尝试过为输入和输出做一个单独的配置类,但我不明白如何从我的主类中调用它:
入出配置:
@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。