我想从我的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
另外..我尝试为输入和输出做一个单独的配置类,但我不明白如何从我的主类调用它:
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。
请帮忙!!感谢大家! 费德里科
答案 0 :(得分:1)
以下代码有效(我相信你在src / main / resources中有你的application.properties文件):
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.getProperty("spring.datasource.driverClassName");
或者您可以将上面的代码放在不同的类中并调用方法来获取属性。所以,你可以随时随地使用它。