3

我想从我的 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。

4

1 回答 1

1

以下代码有效(我相信您的 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
            }

将它放在您的 main 方法中,您可以从“properties”变量中读取值。像:properties.getProperty("spring.datasource.driverClassName");

或者你可以将上面的代码放在不同的类中,调用方法来获取属性。这样您就可以在任何需要的地方使用它。

于 2018-03-26T15:30:10.813 回答