为什么我的" Hello World"春季批量应用程序不处理?

时间:2017-06-30 14:51:37

标签: java spring spring-batch

我一直在努力制作一个简单的弹簧批量应用,它可以读取CSV并将结果打印到日志中。我不能把这件事搞定。我真的很感激任何关于我做错了什么的信息。

我做了一个github回购:https://github.com/rtconner/hello

我希望看到处理器中的Converting (" + person + ") into (" + transformedPerson + ")行。此外,我希望看到从LogItemWriter打印到日志的日志行。我从来没有看到任何一个打印出来。

感谢您对此提供任何帮助。

1 个答案:

答案 0 :(得分:2)

您在项目中犯了多个错误。

根本原因:

Àpplication.java中,您正在传递配置类,因为它需要成为您的Application.class本身。

SpringApplication.run(Application.class, args);代替SpringApplication.run(BatchConfiguration.class, args);

修复此问题后,您将面临项目中出现的其他多个错误,

  1. 没有设置Spring批处理存储库:每个Spring批处理项目都需要一个作业存储库来运行,并且未在项目中配置。如果您不想使用持久性数据库,则可以使用这些
  2. 在内存数据库中使用

    添加依赖项,compile 'com.h2database:h2'以及Java配置以下,

    @Bean
        public ResourcelessTransactionManager transactionManager() {
            return new ResourcelessTransactionManager();
        }
    
        @Bean
        public MapJobRepositoryFactoryBean mapJobRepositoryFactory(ResourcelessTransactionManager txManager)
                throws Exception {
            MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(txManager);
            factory.afterPropertiesSet();
            return factory;
        }
    
        @Bean
        public JobRepository jobRepository(MapJobRepositoryFactoryBean factory) throws Exception {
            return factory.getObject();
        }
    
        @Bean
        public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
            SimpleJobLauncher launcher = new SimpleJobLauncher();
            launcher.setJobRepository(jobRepository);
            return launcher;
        }
    

    我没有尝试过,但如果您的类路径中存在H2或HSQLDB,可能就不需要上面的配置了。为了完整起见我加了。

    1. 不正确的hibernate验证器依赖:这下面的依赖不是hibernate验证器而是hibernate核心,
    2. compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.10.Final'而你需要的是,

      compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.2.1.Final'

      我在修好这些东西后运行你的应用程序,下面是日志,

        .   ____          _            __ _ _
       /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
      ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
       \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
        '  |____| .__|_| |_|_| |_\__, | / / / /
       =========|_|==============|___/=/_/_/_/
       :: Spring Boot ::        (v1.5.4.RELEASE)
      
      2017-07-02 15:02:15.966  INFO 4872 --- [           main] hello.Application                        : Starting Application on Bi-amma with PID 4872 (E:\GithubSamples\hello-master\bin started by kaneez in E:\GithubSamples\hello-master)
      2017-07-02 15:02:15.971  INFO 4872 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
      2017-07-02 15:02:16.114  INFO 4872 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@45dd4eda: startup date [Sun Jul 02 15:02:16 IST 2017]; root of context hierarchy
      2017-07-02 15:02:16.786  INFO 4872 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=batchConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [hello/BatchConfiguration.class]]
      2017-07-02 15:02:16.788  INFO 4872 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'jobRepository' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=jobRepository; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=batchConfiguration; factoryMethodName=jobRepository; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [hello/BatchConfiguration.class]]
      2017-07-02 15:02:16.788  INFO 4872 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'jobLauncher' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=jobLauncher; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=batchConfiguration; factoryMethodName=jobLauncher; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [hello/BatchConfiguration.class]]
      2017-07-02 15:02:17.273  WARN 4872 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @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.
      2017-07-02 15:02:17.301  WARN 4872 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @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.
      2017-07-02 15:02:17.922  INFO 4872 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
      2017-07-02 15:02:17.994  INFO 4872 --- [           main] hello.BatchConfiguration                 : Build Job
      2017-07-02 15:02:17.996  INFO 4872 --- [           main] hello.BatchConfiguration                 : Build Step
      2017-07-02 15:02:18.727  INFO 4872 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from class path resource [org/springframework/batch/core/schema-h2.sql]
      2017-07-02 15:02:18.826  INFO 4872 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from class path resource [org/springframework/batch/core/schema-h2.sql] in 98 ms.
      2017-07-02 15:02:19.130  INFO 4872 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
      2017-07-02 15:02:19.153  INFO 4872 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
      2017-07-02 15:02:19.164  INFO 4872 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: H2
      2017-07-02 15:02:19.346  INFO 4872 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
      2017-07-02 15:02:19.447  INFO 4872 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{run.id=1}]
      2017-07-02 15:02:19.508  INFO 4872 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step]
      Converting (firstName: Jill, lastName: Doe) into (firstName: JILL, lastName: DOE)
      2017-07-02 15:02:19.557  INFO 4872 --- [           main] hello.PersonItemProcessor                : Converting (firstName: Jill, lastName: Doe) into (firstName: JILL, lastName: DOE)
      2017-07-02 15:02:19.558  INFO 4872 --- [           main] hello.LogItemWriter                      : [firstName: JILL, lastName: DOE]
      Converting (firstName: Joe, lastName: Doe) into (firstName: JOE, lastName: DOE)
      2017-07-02 15:02:19.565  INFO 4872 --- [           main] hello.PersonItemProcessor                : Converting (firstName: Joe, lastName: Doe) into (firstName: JOE, lastName: DOE)
      2017-07-02 15:02:19.565  INFO 4872 --- [           main] hello.LogItemWriter                      : [firstName: JOE, lastName: DOE]
      Converting (firstName: Justin, lastName: Doe) into (firstName: JUSTIN, lastName: DOE)
      2017-07-02 15:02:19.573  INFO 4872 --- [           main] hello.PersonItemProcessor                : Converting (firstName: Justin, lastName: Doe) into (firstName: JUSTIN, lastName: DOE)
      2017-07-02 15:02:19.573  INFO 4872 --- [           main] hello.LogItemWriter                      : [firstName: JUSTIN, lastName: DOE]
      Converting (firstName: Jane, lastName: Doe) into (firstName: JANE, lastName: DOE)
      2017-07-02 15:02:19.579  INFO 4872 --- [           main] hello.PersonItemProcessor                : Converting (firstName: Jane, lastName: Doe) into (firstName: JANE, lastName: DOE)
      2017-07-02 15:02:19.579  INFO 4872 --- [           main] hello.LogItemWriter                      : [firstName: JANE, lastName: DOE]
      Converting (firstName: John, lastName: Doe) into (firstName: JOHN, lastName: DOE)
      2017-07-02 15:02:19.586  INFO 4872 --- [           main] hello.PersonItemProcessor                : Converting (firstName: John, lastName: Doe) into (firstName: JOHN, lastName: DOE)
      2017-07-02 15:02:19.588  INFO 4872 --- [           main] hello.LogItemWriter                      : [firstName: JOHN, lastName: DOE]
      2017-07-02 15:02:19.625  INFO 4872 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]
      2017-07-02 15:02:19.653  INFO 4872 --- [           main] hello.Application                        : Started Application in 4.332 seconds (JVM running for 6.183)
      2017-07-02 15:02:19.658  INFO 4872 --- [       Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@45dd4eda: startup date [Sun Jul 02 15:02:16 IST 2017]; root of context hierarchy
      2017-07-02 15:02:19.665  INFO 4872 --- [       Thread-3] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
      

      我在处理器中添加了一个sysout,因此可能会重复消息。