Java Spring批处理类装入器冲突问题

Java Spring批处理类装入器冲突问题,java,spring,spring-batch,classloader,Java,Spring,Spring Batch,Classloader,我正在尝试测试spring batch的xmlreader,但我面临下一个问题: java.lang.ClassCastException: class org.vl.batch.domain.CustomerXml cannot be cast to class org.vl.batch.domain.CustomerXml (org.vl.batch.domain.CustomerXml is in unnamed module of loader 'app'; org.vl.batc

我正在尝试测试spring batch的
xml
reader,但我面临下一个问题:

java.lang.ClassCastException: class org.vl.batch.domain.CustomerXml cannot be cast to class org.vl.batch.domain.CustomerXml
 (org.vl.batch.domain.CustomerXml is in unnamed module of loader 'app';
  org.vl.batch.domain.CustomerXml is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @7129c8ee)
  at org.vl.batch.configuration.JobConfiguration.lambda$customerItemWriter$0(JobConfiguration.java:73) ~[classes/:na]
问题: 如何在不排除开发工具的情况下解决此问题

项目详情 环境:

  • 梯度5.4.1
  • JVM 11.0.2
  • Ubuntu 18.04
项目:

Dto

主类

@SpringBootApplication
@EnableBatchProcessing
public class ReadingXmlApplication {

  public static void main(String[] args) {
    SpringApplication.run(ReadingXmlApplication.class, args);
  }
}
批量配置

@Configuration
@Slf4j
public class JobConfiguration {

  @Autowired
  public JobBuilderFactory jobBuilderFactory;
  @Autowired
  public StepBuilderFactory stepBuilderFactory;

  @Bean
  public StaxEventItemReader<CustomerXml> customerItemReader() {
    XStreamMarshaller unmarshaller = new XStreamMarshaller();
    Map<String, Class> aliases = new HashMap<>();
    aliases.put("customer", CustomerXml.class);
    unmarshaller.setAliases(aliases);
    StaxEventItemReader<CustomerXml> reader = new StaxEventItemReader<>();
    reader.setResource(new ClassPathResource("/data/customers.xml"));
    reader.setFragmentRootElementName("customer");
    reader.setUnmarshaller(unmarshaller);
    return reader;
  }

  @Bean
  public ItemWriter<CustomerXml> customerItemWriter() {
    return items -> items.forEach(item -> log.info("item:\t\t{}", item));
  }

  @Bean
  public Step step1() {
    return stepBuilderFactory.get("step1")
        .allowStartIfComplete(true)
        .<CustomerXml, CustomerXml>chunk(10)
        .reader(customerItemReader())
        .writer(customerItemWriter())
        .build();
  }

  @Bean
  public Job job() {
    return jobBuilderFactory.get("job")
        .start(step1())
        .build();
  }
}
总的来说

  • 使用IDE:
  • 使用终端:

  • 只是想知道。。没有任何处理器对您合适吗?我已经提供了尽可能少的数据来重现这个问题。所有未受影响的部件-丢失。如何启动应用程序?错误消息看起来您正在使用模块path@SimonMartinelli答案被添加到问题细节中。
    @SpringBootApplication
    @EnableBatchProcessing
    public class ReadingXmlApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(ReadingXmlApplication.class, args);
      }
    }
    
    @Configuration
    @Slf4j
    public class JobConfiguration {
    
      @Autowired
      public JobBuilderFactory jobBuilderFactory;
      @Autowired
      public StepBuilderFactory stepBuilderFactory;
    
      @Bean
      public StaxEventItemReader<CustomerXml> customerItemReader() {
        XStreamMarshaller unmarshaller = new XStreamMarshaller();
        Map<String, Class> aliases = new HashMap<>();
        aliases.put("customer", CustomerXml.class);
        unmarshaller.setAliases(aliases);
        StaxEventItemReader<CustomerXml> reader = new StaxEventItemReader<>();
        reader.setResource(new ClassPathResource("/data/customers.xml"));
        reader.setFragmentRootElementName("customer");
        reader.setUnmarshaller(unmarshaller);
        return reader;
      }
    
      @Bean
      public ItemWriter<CustomerXml> customerItemWriter() {
        return items -> items.forEach(item -> log.info("item:\t\t{}", item));
      }
    
      @Bean
      public Step step1() {
        return stepBuilderFactory.get("step1")
            .allowStartIfComplete(true)
            .<CustomerXml, CustomerXml>chunk(10)
            .reader(customerItemReader())
            .writer(customerItemWriter())
            .build();
      }
    
      @Bean
      public Job job() {
        return jobBuilderFactory.get("job")
            .start(step1())
            .build();
      }
    }
    
    java.lang.ClassCastException: class org.example.springbatchclassloaderconflict.model.HeroXml cannot be cast to class org.example.springbatchclassloaderconflict.model.HeroXml (org.example.springbatchclassloaderconflict.model.HeroXml is in unnamed module of loader 'app'; org.example.springbatchclassloaderconflict.model.HeroXml is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @674d3da3)
    
    /home/sergii/.sdkman/candidates/java/current/bin/java -javaagent:/home/sergii/IDE/idea-IC-191.7479.19/lib/idea_rt.jar=34191:/home/sergii/IDE/idea-IC-191.7479.19/bin -Dfile.encoding=UTF-8 -classpath /home/sergii/Development/projects/my/spring/spring-batch-class-loader-conflict/out/production/classes:/home/sergii/Development/projects/my/spring/spring-batch-class-loader-conflict/out/production/resources:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-batch/2.2.0.M3/fe0bb4352d0c49d9b088db13c6a89fd9619bf2dc/spring-boot-starter-batch-2.2.0.M3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-devtools/2.2.0.M3/853cb206490a2946646d4bac02446b8dc564be30/spring-boot-devtools-2.2.0.M3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/com.thoughtworks.xstream/xstream/1.4.11.1/6c120c45a8c480bb2fea5b56502e3993ddd74fd2/xstream-1.4.11.1.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-oxm/5.1.7.RELEASE/5436d5d6d56b70768d296ca691d37c533429fea0/spring-oxm-5.1.7.RELEASE.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/com.h2database/h2/1.4.199/7bf08152984ed8859740ae3f97fae6c72771ae45/h2-1.4.199.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-jdbc/2.2.0.M3/fbd92e6461e5e37186c360f0080af27719780811/spring-boot-starter-jdbc-2.2.0.M3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter/2.2.0.M3/d858f3131933381d6661c0f08b6bd9669f581123/spring-boot-starter-2.2.0.M3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.batch/spring-batch-core/4.2.0.M2/60b52cb2d85ead44ecf7b0bf47dfeb6e672316b6/spring-batch-core-4.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/mysql/mysql-connector-java/8.0.16/6088b7a25188ab4b3ab865422a8ec77ade29236/mysql-connector-java-8.0.16.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/xmlpull/xmlpull/1.1.3.1/2b8e230d2ab644e4ecaa94db7cdedbc40c805dfa/xmlpull-1.1.3.1.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/xpp3/xpp3_min/1.1.4c/19d4e90b43059058f6e056f794f0ea4030d60b86/xpp3_min-1.1.4c.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jdbc/5.2.0.M2/ea5a41f2b01a2a5f88426e3a509f53479e2b6c41/spring-jdbc-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.2.0.M3/3d990f3a7716875013570a1ddd9c79a5dc556390/spring-boot-autoconfigure-2.2.0.M3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.2.0.M3/fc0b424da418b242c3d953bd6bbf06f03bfb1925/spring-boot-2.2.0.M3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/5.2.0.M2/c85d8095c7765d8d38b9b6a357aa347b617bab79/spring-context-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/5.2.0.M2/8deb00a5c5e18b6c594877afe4e82b8b8d64ccb/spring-aop-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/5.2.0.M2/7042f477e471796be677f71ec63575ac7c47a749/spring-tx-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.batch/spring-batch-infrastructure/4.2.0.M2/b522e7c7f1c5ebb575136f149b0408462d391e22/spring-batch-infrastructure-4.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.retry/spring-retry/1.2.4.RELEASE/e5a1e629b2743dc7bbe4a8d07ebe9ff6c3b816ce/spring-retry-1.2.4.RELEASE.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/5.2.0.M2/c4aa2bb803602ebc26a7ee47628f6af106e1bf55/spring-beans-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.2.0.M2/9a84d456ad8d5151da06fc8a85540da9cc95d734/spring-core-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/5.2.0.M2/cdf6909ed2decf704486ca85395e97177fd7535b/spring-expression-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-logging/2.2.0.M3/ae4dc76f8f14327ca3a792584f666d484f21b5/spring-boot-starter-logging-2.2.0.M3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/jakarta.annotation/jakarta.annotation-api/1.3.4/a858ec3f0ebd2b8d855c1ddded2cde9b381b0517/jakarta.annotation-api-1.3.4.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/com.zaxxer/HikariCP/3.3.1/bb447db60818ecfdbb1b99e7bd096ba7a252d91a/HikariCP-3.3.1.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.9.8/11283f21cc480aa86c4df7a0a3243ec508372ed2/jackson-databind-2.9.8.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/io.micrometer/micrometer-core/1.1.4/96eabfe2343a4a4676d215b2122cbbc4d4b6af9b/micrometer-core-1.1.4.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/javax.batch/javax.batch-api/1.0/65392d027a6eb369fd9fcd1b75cae150e25ac03c/javax.batch-api-1.0.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.yaml/snakeyaml/1.24/13a9c0d6776483c3876e3ff9384f9bb55b17001b/snakeyaml-1.24.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.codehaus.jettison/jettison/1.2/765a6181653f4b05c18c7a9e8f5c1f8269bf9b2/jettison-1.2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-to-slf4j/2.11.2/6d37bf7b046c0ce2669f26b99365a2cfa45c4c18/log4j-to-slf4j-2.11.2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.slf4j/jul-to-slf4j/1.7.26/8031352b2bb0a49e67818bf04c027aa92e645d5c/jul-to-slf4j-1.7.26.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.26/77100a62c2e6f04b53977b9f541044d7d722693d/slf4j-api-1.7.26.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.2.0.M2/988d5bac4a51ed2675626378ee79f8447eda2002/spring-jcl-5.2.0.M2.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.9.0/7c10d545325e3a6e72e06381afe469fd40eb701/jackson-annotations-2.9.0.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.9.8/f5a654e4675769c716e5b387830d19b501ca191/jackson-core-2.9.8.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.hdrhistogram/HdrHistogram/2.1.9/e4631ce165eb400edecfa32e03d3f1be53dee754/HdrHistogram-2.1.9.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.latencyutils/LatencyUtils/2.0.3/769c0b82cb2421c8256300e907298a9410a2a3d3/LatencyUtils-2.0.3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-core/1.2.3/864344400c3d4d92dfeb0a305dc87d953677c03c/logback-core-1.2.3.jar:/home/sergii/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.11.2/f5e9a2ffca496057d6891a3de65128efc636e26e/log4j-api-2.11.2.jar org.example.springbatchclassloaderconflict.ReadingHeroesFromXmlApplication
    
    gradle bootRun