Spring Boot spring.batch.job.enabled = false无法识别 [英] Spring boot spring.batch.job.enabled=false not able to recognize

查看:565
本文介绍了Spring Boot spring.batch.job.enabled = false无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried spring.batch.job.enabled=false in application.properties and -Dspring.batch.job.enabled=false when running the jar file.

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

However @EnableBatchProcessing automatically start running the batch jobs on application start. How i can debug such scenario?

TestConfiguration.class

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

MainApplication

@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而不是父项

pom.xml I am using dependency as spring boot and not as parent

<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应用程序启动时,实际上尝试在我编写了一些应用程序逻辑的地方对此自定义bean进行依赖项注入.

I was able to know whats going on, I am using custom reader/processor/writer. When springboot application starts it actually try to do dependency injection of this custom beans beans where I have written some application logic.

示例

** TestConfiguration.class **

** 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,它也将创建文件,为了克服这一点,我创建了自定义逻辑来注入或不注入bean,如下所示:

It will create the file even with spring.batch.job.enabled=false, to overcome this I have created custom logic to inject the beans or not as below

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");

}

这篇关于Spring Boot spring.batch.job.enabled = false无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆