【发布时间】:2018-04-15 13:39:51
【问题描述】:
我正在使用 Spring Batch 而不使用 Spring Boot 设置一个项目。 当创建 Spring 应用上下文时,所有的作业都会被执行。
我尝试将spring.batch.job.enbled=false 添加到 application.properties 以防止这种情况发生,但它仍然不起作用。
还有其他方法可以阻止 Spring 在启动时执行作业吗?
主类:
package com.project.batch;
import ...
@Configuration
@EnableBatchProcessing
@PropertySource("classpath:application.properties")
public class App {
public static void main(String [] args) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
System.out.println("starting main");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.project.batch");
context.refresh();
//JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
//JobLauncher jobLauncher = context.getBean(JobLauncher.class);
//JobExecution execution = jobLauncher.run(context.getBean("loaderJob",Job.class),jobParameters);
System.out.println("finished!!");
}
}
职位类别:
package com.project.batch;
import ...
@Configuration
public class LoaderJobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job loaderJob(Step step1) throws Exception {
System.out.println("Starting loaderJob");
...
}
}
application.properties:
spring.batch.job.enbled=false
spring.batch.job.names=
运行日志:
starting main
Nov 06, 2017 9:29:02 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@306a30c7: startup date [Mon Nov 06 09:29:02 EST 2017]; root of context hierarchy
Nov 06, 2017 9:29:03 AM org.springframework.context.annotation.ConfigurationClassEnhancer intercept
WARNING: @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details
Nov 06, 2017 9:29:03 AM org.springframework.context.annotation.ConfigurationClassEnhancer intercept
WARNING: @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details
Nov 06, 2017 9:29:03 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.postgresql.Driver
Starting loaderJob
found the value: [MER]
Completed loaderJob
finished!!
Process finished with exit code 0
编辑:从主类中删除作业执行代码,作业仍然在上下文刷新时触发
编辑 2:包括运行日志
编辑 3:修正错字并更新日志
【问题讨论】:
-
您的主要方法是执行作业。如果您不希望 It 在启动时执行,请删除那里的代码...
-
@MichaelMinella 我已经删除了作业执行代码,但作业仍然被触发
-
你能提供显示开始的日志吗?
-
现在添加日志
-
您的属性中有错字:应该是
spring.batch.job.enabled=false,而不是enbled
标签: spring spring-boot spring-batch spring-annotations spring-config