Spring Boot 4.0.6与 Spring Batch 6:Spring Batch的表未被创建

后端开发 2026-07-09

我正在使用Spring Boot 4.0.6,搭配Spring Batch和通过Docker运行的MySQL;然而,在 spring_batch 模式中的Spring Batch表(例如BATCH_JOB_EXECUTION等)未能自动创建。

以下是源代码和配置:

package com.dom.project.batch.batch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.dom.project.batch.batch.*")
public class BatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }

}
package com.dom.project.batch.batch.config;

import org.springframework.batch.core.job.Job;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.Step;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.infrastructure.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;


@Configuration
public class SampleJob {

    @Bean
    public Flow mainFlow(Step firstStep, Step thirdStep) {
        return new FlowBuilder<Flow>("mainFlow")
                .start(firstStep) //“Il primo Step da eseguire in questo flow del Job”
                .next(thirdStep)//“Il secondo Step da eseguire in questo flow del Job”
                .end(); //“Il flow termina qui”
    }


    @Bean
    public Job firstJob(JobRepository jobRepository,
                        Flow mainFlow) {

        Job firstJob = new JobBuilder("firstJob", jobRepository)
                .start(mainFlow) //“Il flow da eseguire in questo Job”
                .end() //“Il Job termina qui”
                .build(); // creo l'oggetto Job
        return firstJob; //return lo consegna a Spring
    }

    @Bean
    public Step firstStep(JobRepository jobRepository,
                          PlatformTransactionManager transactionManager) { //motore transazionale di Spring Batch

        return new StepBuilder("firstStep", jobRepository)
                .tasklet(firstTask(), transactionManager)
                .build();
    }

    @Bean
    public Step thirdStep(JobRepository jobRepository,
                          PlatformTransactionManager transactionManager) { //motore transazionale di Spring Batch

        return new StepBuilder("thirdStep", jobRepository)
                .tasklet(thirdTask(), transactionManager)
                .build();
    }

    private Tasklet firstTask() {
        return (contribution, chunkContext) -> {
            System.out.println("This is first tasklet step");
            return RepeatStatus.FINISHED;
        };
    }

    private Tasklet thirdTask() {
        return (contribution, chunkContext) -> {
            System.out.println("This is third tasklet step");
            return RepeatStatus.FINISHED;
        };
    }
}
server:
  port: 8080

spring:
  application:
    name: batch

  datasource:
    url: jdbc:mysql://localhost:3310/spring_batch?useSSL=false&allowPublicKeyRetrieval=true&autocommit=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

  sql:
    init:
      mode: always
      #schema-locations: classpath:org/springframework/batch/core/schema-mysql.sql

  batch:
    jdbc:
      initialize-schema: always
    job:
      enabled: true

logging:
  level:
    org:
      springframework:
        batch: DEBUG
        jdbc: DEBUG

以下是日志:

:: Spring Boot ::                (v4.0.6)

2026-05-09T16:59:44.044+02:00  INFO 8108 --- [batch] [           main] c.d.p.batch.batch.BatchApplication       : Starting BatchApplication using Java 21.0.8 with PID 8108 (/Users/domenicoiaderosa/Documents/batch/target/classes started by domenicoiaderosa in /Users/domenicoiaderosa/Documents/batch)
2026-05-09T16:59:44.050+02:00  INFO 8108 --- [batch] [           main] c.d.p.batch.batch.BatchApplication       : No active profile set, falling back to 1 default profile: "default"
2026-05-09T16:59:44.619+02:00  INFO 8108 --- [batch] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2026-05-09T16:59:44.636+02:00  INFO 8108 --- [batch] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces.
2026-05-09T16:59:44.955+02:00  INFO 8108 --- [batch] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2026-05-09T16:59:45.323+02:00  INFO 8108 --- [batch] [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@d5af0a5
2026-05-09T16:59:45.324+02:00  INFO 8108 --- [batch] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2026-05-09T16:59:45.363+02:00  INFO 8108 --- [batch] [           main] org.hibernate.orm.jpa                    : HHH008540: Processing PersistenceUnitInfo [name: default]
2026-05-09T16:59:45.424+02:00  INFO 8108 --- [batch] [           main] org.hibernate.orm.core                   : HHH000001: Hibernate ORM core version 7.2.12.Final
2026-05-09T16:59:46.058+02:00  INFO 8108 --- [batch] [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2026-05-09T16:59:46.281+02:00  INFO 8108 --- [batch] [           main] org.hibernate.orm.connections.pooling    : HHH10001005: Database info:
    Database JDBC URL [jdbc:mysql://localhost:3310/spring_batch?useSSL=false&allowPublicKeyRetrieval=true&autocommit=true]
    Database driver: MySQL Connector/J
    Database dialect: MySQLDialect
    Database version: 9.4
    Default catalog/schema: spring_batch/undefined
    Autocommit mode: undefined/unknown
    Isolation level: REPEATABLE_READ [default REPEATABLE_READ]
    JDBC fetch size: none
    Pool: DataSourceConnectionProvider
    Minimum pool size: undefined/unknown
    Maximum pool size: undefined/unknown
2026-05-09T16:59:46.718+02:00  INFO 8108 --- [batch] [           main] org.hibernate.orm.core                   : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2026-05-09T16:59:46.723+02:00  INFO 8108 --- [batch] [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2026-05-09T16:59:46.782+02:00 DEBUG 8108 --- [batch] [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
2026-05-09T16:59:46.784+02:00 DEBUG 8108 --- [batch] [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
2026-05-09T16:59:46.793+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.job.AbstractJob           : No ObservationRegistry has been set, defaulting to ObservationRegistry NOOP
2026-05-09T16:59:46.794+02:00 DEBUG 8108 --- [batch] [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
2026-05-09T16:59:46.846+02:00 DEBUG 8108 --- [batch] [           main] ultJobOperatorTransactionAttributeSource : Adding transactional method [public boolean org.springframework.batch.core.launch.support.TaskExecutorJobOperator.stop(org.springframework.batch.core.job.JobExecution) throws org.springframework.batch.core.launch.JobExecutionNotRunningException] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2026-05-09T16:59:46.847+02:00 DEBUG 8108 --- [batch] [           main] ultJobOperatorTransactionAttributeSource : Adding transactional method [public org.springframework.batch.core.job.JobExecution org.springframework.batch.core.launch.support.TaskExecutorJobOperator.abandon(org.springframework.batch.core.job.JobExecution) throws org.springframework.batch.core.launch.JobExecutionAlreadyRunningException] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2026-05-09T16:59:46.847+02:00 DEBUG 8108 --- [batch] [           main] ultJobOperatorTransactionAttributeSource : Adding transactional method [public org.springframework.batch.core.job.JobExecution org.springframework.batch.core.launch.support.TaskExecutorJobOperator.recover(org.springframework.batch.core.job.JobExecution)] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2026-05-09T16:59:46.983+02:00  INFO 8108 --- [batch] [           main] c.d.p.batch.batch.BatchApplication       : Started BatchApplication in 3.632 seconds (process running for 4.188)
2026-05-09T16:59:46.987+02:00  INFO 8108 --- [batch] [           main] o.s.b.b.a.JobLauncherApplicationRunner   : Running default command line with: []
2026-05-09T16:59:47.070+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.c.l.s.TaskExecutorJobLauncher      : Creating a new job instance for job = firstJob with parameters = {}
2026-05-09T16:59:47.073+02:00  INFO 8108 --- [batch] [           main] o.s.b.c.l.s.TaskExecutorJobLauncher      : Job: [FlowJob: [name=firstJob]] launched with the following parameters: [{}]
2026-05-09T16:59:47.073+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.job.AbstractJob           : Job execution starting: JobExecution: id=1, version=null, startTime=null, endTime=null, lastUpdated=null, status=STARTING, exitStatus=exitCode=UNKNOWN;exitDescription=, job=[JobInstance: id=1, version=null, Job=[firstJob]], jobParameters=[{}]
2026-05-09T16:59:47.077+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.core.job.flow.support.SimpleFlow   : Resuming state=mainFlow.step0 with status=UNKNOWN
2026-05-09T16:59:47.078+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.core.job.flow.support.SimpleFlow   : Handling state=mainFlow.step0
2026-05-09T16:59:47.079+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Executing: id=1
2026-05-09T16:59:47.080+02:00  INFO 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Executing step: [firstStep]
2026-05-09T16:59:47.082+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.i.repeat.support.RepeatTemplate    : Starting repeat context.
2026-05-09T16:59:47.083+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.i.repeat.support.RepeatTemplate    : Repeat operation about to start at count=1
2026-05-09T16:59:47.083+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.c.s.c.StepContextRepeatCallback    : Preparing chunk execution for StepContext: org.springframework.batch.core.scope.context.StepContext@395197cb
2026-05-09T16:59:47.083+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.c.s.c.StepContextRepeatCallback    : Chunk execution starting: queue size=0
This is first tasklet step
2026-05-09T16:59:47.145+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.tasklet.TaskletStep  : Applying contribution: [StepContribution: read=0, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
2026-05-09T16:59:47.146+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.tasklet.TaskletStep  : Saving step execution before commit: StepExecution: id=1, version=0, name=firstStep, status=STARTED, exitStatus=EXECUTING, readCount=0, filterCount=0, writeCount=0, readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
2026-05-09T16:59:47.157+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.i.repeat.support.RepeatTemplate    : Repeat is complete according to policy and result value.
2026-05-09T16:59:47.158+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Step execution success: id=1
2026-05-09T16:59:47.158+02:00  INFO 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Step: [firstStep] executed in 77ms
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Step execution complete: StepExecution: id=1, version=0, name=firstStep, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0, readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.core.job.flow.support.SimpleFlow   : Completed state=mainFlow.step0 with status=COMPLETED
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.core.job.flow.support.SimpleFlow   : Handling state=mainFlow.step1
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Executing: id=2
2026-05-09T16:59:47.159+02:00  INFO 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Executing step: [thirdStep]
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.i.repeat.support.RepeatTemplate    : Starting repeat context.
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.i.repeat.support.RepeatTemplate    : Repeat operation about to start at count=1
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.c.s.c.StepContextRepeatCallback    : Preparing chunk execution for StepContext: org.springframework.batch.core.scope.context.StepContext@7030b74c
2026-05-09T16:59:47.159+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.c.s.c.StepContextRepeatCallback    : Chunk execution starting: queue size=0
This is third tasklet step
2026-05-09T16:59:47.161+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.tasklet.TaskletStep  : Applying contribution: [StepContribution: read=0, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
2026-05-09T16:59:47.161+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.tasklet.TaskletStep  : Saving step execution before commit: StepExecution: id=2, version=0, name=thirdStep, status=STARTED, exitStatus=EXECUTING, readCount=0, filterCount=0, writeCount=0, readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
2026-05-09T16:59:47.163+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.i.repeat.support.RepeatTemplate    : Repeat is complete according to policy and result value.
2026-05-09T16:59:47.163+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Step execution success: id=2
2026-05-09T16:59:47.163+02:00  INFO 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Step: [thirdStep] executed in 3ms
2026-05-09T16:59:47.163+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.step.AbstractStep         : Step execution complete: StepExecution: id=2, version=0, name=thirdStep, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0, readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0
2026-05-09T16:59:47.163+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.core.job.flow.support.SimpleFlow   : Completed state=mainFlow.step1 with status=COMPLETED
2026-05-09T16:59:47.163+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.core.job.flow.support.SimpleFlow   : Handling state=mainFlow.COMPLETED
2026-05-09T16:59:47.163+02:00 DEBUG 8108 --- [batch] [           main] o.s.b.core.job.flow.support.SimpleFlow   : Completed state=mainFlow.COMPLETED with status=COMPLETED
2026-05-09T16:59:47.163+02:00 DEBUG 8108 --- [batch] [           main] o.s.batch.core.job.AbstractJob           : Job execution complete: JobExecution: id=1, version=null, startTime=2026-05-09T16:59:47.077349, endTime=null, lastUpdated=2026-05-09T16:59:47.163619, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=1, version=null, Job=[firstJob]], jobParameters=[{}]
2026-05-09T16:59:47.165+02:00  INFO 8108 --- [batch] [           main] o.s.b.c.l.s.TaskExecutorJobLauncher      : Job: [FlowJob: [name=firstJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 86ms
2026-05-09T16:59:47.169+02:00  INFO 8108 --- [batch] [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2026-05-09T16:59:47.172+02:00  INFO 8108 --- [batch] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2026-05-09T16:59:47.180+02:00  INFO 8108 --- [batch] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

我该如何解决?

解决方案

你可能需要使用新的注解 @EnableJdbcJobRepository 来为Spring Batch启用JDBC仓库。

有关更多信息,请参阅其文档 https://docs.spring.io/spring-batch/reference/whatsnew.html#_new_annotations_and_classes_for_batch_infrastructure_configuration

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章