gpt4 book ai didi

java - Spring boot spring.batch.job.enabled=false 无法识别

转载 作者:搜寻专家 更新时间:2023-11-01 03:20:43 25 4
gpt4 key购买 nike

我在运行 jar 文件时在 application.properties 和 -Dspring.batch.job.enabled=false 中尝试了 spring.batch.job.enabled=false

但是 @EnableBatchProcessing 在应用程序启动时自动开始运行批处理作业。我如何调试这种情况?

TestConfiguration.class

@Configuration
@EnableBatchProcessing
public class TestConfiguration {...}

主应用程序

@ComponentScan("com.demo")
@EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) throws BeansException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException, JobRestartException {

ConfigurableApplicationContext ctx = SpringApplication.run(TestConfiguration.class, args);
...}

pom.xml我将依赖项用作 spring boot 而不是父项

<dependencyManagement>
<dependencies>
<!-- Import dependecy for spring boot from here-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

最佳答案

我能够知道发生了什么,我正在使用自定义读取器/处理器/写入器。当 springboot 应用程序启动时,它实际上会尝试对该自定义 beans bean 进行依赖注入(inject),我已经在其中编写了一些应用程序逻辑。

例子

** TestConfiguration.class**

    @Configuration
@EnableBatchProcessing
public class TestConfiguration {

@Bean
@Conditional(Employee.class)
public ItemWriter<Employee> writer_employee(DataSource dataSource) throws IOException {
FlatFileItemWriter<Employee> writer = new FlatFileItemWriter<Employee>();
writer.setResource(new FileSystemResource(FinanceReportUtil.createFile("Employee.csv")));
writer.setHeaderCallback(new FlatFileHeaderCallback() {
@Override
public void writeHeader(Writer writer) throws IOException {
writer.write("id, name");
}
});
DelimitedLineAggregator<Employee> delLineAgg = new DelimitedLineAggregator<Employee>();
delLineAgg.setDelimiter(",");
BeanWrapperFieldExtractor<Employee> fieldExtractor = new BeanWrapperFieldExtractor<Employee>();
fieldExtractor.setNames(new String[]{"id", "name"});
delLineAgg.setFieldExtractor(fieldExtractor);
writer.setLineAggregator(delLineAgg);
return writer;
}

@Bean
@Conditional(Manager.class)
public ItemWriter<Person> writer_manager(DataSource dataSource) throws IOException {

// Does the same logic as employee
}

// Also has job and step etc.
}

即使使用 spring.batch.job.enabled=false,它也会创建文件,为了克服这个问题,我创建了自定义逻辑来注入(inject)或不注入(inject) bean,如下所示

application.properties

# all, manager, employee
person=manager

ManagerCondition.class

public class ManagerCondition implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String person= context.getEnvironment().getProperty("person");
return person.equals("manager");

}

关于java - Spring boot spring.batch.job.enabled=false 无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31276011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com