如何在Spring批处理中读取application.properties并设置为作业参数
我想从application.properties中读取Spring批处理应用程序的输入和输出路径,并将它们设置为jobParametersBuilder,以便在整个作业执行过程中访问它们(将它们分配给读卡器和写卡器) 我能够在其他配置类中读取application.properties,但在我的主类中似乎无法实现。我需要在这里这样做,以便能够在执行作业之前将值分配给作业参数 我的主课:如何在Spring批处理中读取application.properties并设置为作业参数,spring,spring-boot,spring-batch,application.properties,Spring,Spring Boot,Spring Batch,Application.properties,我想从application.properties中读取Spring批处理应用程序的输入和输出路径,并将它们设置为jobParametersBuilder,以便在整个作业执行过程中访问它们(将它们分配给读卡器和写卡器) 我能够在其他配置类中读取application.properties,但在我的主类中似乎无法实现。我需要在这里这样做,以便能够在执行作业之前将值分配给作业参数 我的主课: @SpringBootApplication public class GleBatchApplicatio
@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("=================================================");
我的应用程序.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
此外,我还尝试为输入和输出创建一个单独的配置类,但我不知道如何从主类调用它:
InOutConfiguration:
@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中):
将它放在main方法中,就可以从“properties”变量中读取值。比如:properties.getProperty(“spring.datasource.driverClassName”)代码>
或者,您可以将上述代码放在不同的类中,并调用该方法来获取属性。这样您就可以随时随地使用它。您好,客人。这是有道理的,我今天会尝试一下,并告诉你它是如何进行的。非常感谢你的回答,它很有魅力。现在,我想通过CommandLineJobRunner传递输入和输出路径,并将它们存储在application.properties的file.input和file.output中。我怎样才能做到这一点?提前非常感谢你@我不知道你为什么喜欢覆盖application.properties。因为,同样,您需要读取文件。相反,阅读代码中的命令行args,并将args设置为系统属性,如system.setProperty(“name”,value);并在任何需要的地方使用System.getProperty(“名称”);要解析命令行参数,请参见org.apache.commons.cli.commandlineparseries示例,您的回答是正确的。我把我正在考虑的选择搞混了。其中之一是通过命令行将路径传递到jobParameters,并在应用程序中使用它们。谢谢你的帮助!
Properties properties = new Properties();
InputStream file = null;
try {
file = GleBatchApplication.class.getClassLoader().getResourceAsStream("application.properties");
properties.load(file);
}catch(Exception e) {
//exception handling
}