【问题标题】:Spring Batch-MySQL issueSpring Batch-MySQL 问题
【发布时间】:2018-07-07 15:55:12
【问题描述】:

当我测试我的 Spring Batch (v4.0.1) 应用程序时。在内存数据库中使用 H2 它就像一个魅力,但是当我使用 MySQL 时它无法运行 CommandLineRunner 有什么解释吗?

我的 POM 文件:

  <dependencies>

    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
         <groupId>org.jsoup</groupId>
         <artifactId>jsoup</artifactId>
         <version>1.11.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>   
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

application.properties:

 spring.jpa.hibernate.ddl-auto=update
 spring.jpa.show-sql=true
 spring.jpa.open-in-view=false
 spring.datasource.url=jdbc:Mysql://localhost/myDB?useSSL=false
 spring.datasource.username=root
 spring.datasource.password=87654321
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

控制台:

    main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    main] o.s.b.a.batch.JpaBatchConfigurer         : JPA does not support custom isolation levels, so locks may not be taken when launching Jobs
    main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: MYSQL
    main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
    main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
    main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
    main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    main] io.nader.org.ScrapOneApplication         : Started ScrapOneApplication in 65.255 seconds (JVM running for 76.788)
    main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: [--spring.output.ansi.enabled=always]
    main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
    main] o.s.jdbc.support.SQLErrorCodesFactory    : SQLErrorCodes loaded: [DB2, Derby, H2, HDB, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
    main] ConditionEvaluationReportLoggingListener : 

   Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.        
  2018-07-07 18:08:42.968 ERROR 780 --- [           main] o.s.boot.SpringApplication               : Application run failed

   java.lang.IllegalStateException: Failed to execute CommandLineRunner                                                     
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)

【问题讨论】:

    标签: java mysql hibernate spring-boot spring-batch


    【解决方案1】:

    当你使用@SpringBootApplication 时,Spring 的 autoConfiguration 默认是开启的。由于您在类路径中加载了 Spring Batch 依赖项,因此也启用了 Spring Batch AutoConfiguration。

    在应用程序启动期间,Spring Batch 的自动配置 (BatchAutoConfiguration) 会创建一个 Runner,它会运行 BatchConfig 中定义的所有作业。

    您可以通过在应用程序属性中将 spring.batch.job.enabled 属性设置为 false 或像您一样简单地排除批处理的自动配置来禁用此行为。

    请参阅herehere 了解更多信息。

    改编自https://stackoverflow.com/a/50191789/6572971

    【讨论】:

    • “在应用程序启动期间,BatchAutoConfiguration 创建一个 Runner 并运行 BatchConfig 中定义的所有作业”这是我的应用程序的功能,从文件中读取一些数据并对其进行处理,禁用它行为违背了编写应用程序的目的
    • 解决方案是使用弹簧批处理 JobLauncher :
    猜你喜欢
    • 2017-03-20
    • 2023-03-15
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多