Java 为什么我的spring批处理应用程序无法处理?

Java 为什么我的spring批处理应用程序无法处理?,java,spring,spring-batch,Java,Spring,Spring Batch,我一直在努力制作一个简单的spring批处理应用程序,它可以读取CSV并将结果打印到日志中。我不能让这东西工作。如果你能告诉我我做错了什么,我将不胜感激 我已经进行了github回购: 我希望看到处理器中的将(“+person+”)转换为(“+transformedPerson+”)行。此外,我还希望看到从LogItemWriter打印到日志的日志行。我从来没有看到任何一个被打印出来 感谢您在这方面的帮助。您在项目中犯了多个错误 根本原因: 在ÀApplication.java中,您正在传递配置

我一直在努力制作一个简单的spring批处理应用程序,它可以读取CSV并将结果打印到日志中。我不能让这东西工作。如果你能告诉我我做错了什么,我将不胜感激

我已经进行了github回购:

我希望看到处理器中的
将(“+person+”)转换为(“+transformedPerson+”)
行。此外,我还希望看到从
LogItemWriter
打印到日志的日志行。我从来没有看到任何一个被打印出来


感谢您在这方面的帮助。

您在项目中犯了多个错误

根本原因:

ÀApplication.java
中,您正在传递配置类,因为它需要传递给
应用程序.class
本身

SpringApplication.run(Application.class,args)而不是
SpringApplication.run(BatchConfiguration.class,args)

一旦你解决了这个问题,你将面临在你的项目中错误的多个其他错误

  • 未设置Spring批处理存储库:每个Spring批处理项目都需要一个作业存储库来运行,而该作业存储库未在项目中配置。如果不想使用持久数据库,可以使用内存中的数据库
  • 添加依赖项,
    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,则可能不需要上述配置。为了完整起见,我添加了

  • 错误的hibernate验证程序依赖项:下面的依赖项不是hibernate验证程序,而是hibernate核心
  • 编译组:'org.hibernate',名称:'hibernate core',版本:'5.2.10.Final'
    而您需要的是

    编译组:'org.hibernate',名称:'hibernate validator',版本:'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,因此消息可能会重复。

    将代码添加到问题中以说明问题。同时添加任何错误/问题说明。
    不处理
    没有任何意义。如果我知道问题所在,我会完全添加该代码。我根本不知道是什么导致了这个问题。然后你需要花一些时间,创建一个演示这个问题的模型。很多时候,将一个应用程序简化为一个失败的例子所涉及的工作将足以隔离问题,顺便说一句,你将自己解决它。我非常努力地做到这一点。这个应用程序非常简单…请使用
    --stacktrace--debug
    选项/参数运行gradle(?)构建,并与我们共享一些(相关)输出。