Spring Boot Batch处理XML文件时文件名限制等多问题咨询
问题1:新增非首次匹配文件名的XML文件被忽略
问题原因
未给multiItemReader加@StepScope时,该Bean是全局单例,只会在应用启动时扫描一次指定路径下的文件,后续新增的文件不会被重新扫描识别。
解决方法
- 首先确认资源匹配规则的
inputResources配置为通配符格式:如果要匹配所有p_开头的XML文件,设置为p_*.xml;如果要匹配该目录下所有XML文件,设置为*.xml - 保留
multiItemReader上的@StepScope注解,该注解会让每次Step执行时都重新实例化Reader,重新扫描路径下所有匹配规则的文件,即可解决新增文件被忽略的问题。
问题2:加
@StepScope后移动文件步骤报错 问题原因
@StepScope的作用域是Step执行期间,不能直接标注在Step本身上,Step是Job的固定组成部分,归Job生命周期管理,给Step加该注解会导致Job初始化时找不到活跃的Step上下文,抛出ScopeNotActiveException异常。
解决方法
删除moveFileStep上的@StepScope注解,把资源扫描逻辑下沉到Tasklet层,给Tasklet加@StepScope实现延迟加载:
@Bean @StepScope public FileMovingTasklet fileMovingTasklet(@Value("${input.resources}") String inputResources) { FileMovingTasklet task = new FileMovingTasklet(); ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver(); org.springframework.core.io.Resource[] resources = null; try { resources = patternResolver.getResources(Constantes.PREFIXE + inputResources); } catch(Exception ex) { ex.printStackTrace(); } task.setResources(resources); task.setTargetResources(targetResources); return task; } @Bean public Step moveFileStep() { return stepBuilderFactory.get("moveFile") .tasklet(fileMovingTasklet(null)) .build(); }
该修改会让Tasklet在Step执行时才实例化,既能拿到当前最新的文件列表,也不会触发上下文不存在的错误。
问题3:可执行JAR运行时XML解析内容为空
问题原因
这是JAXB类加载机制差异导致的:Eclipse运行时是目录类加载,能正常识别实体类上的JAXB注解;Spring Boot可执行JAR是嵌套JAR类加载,默认的Jaxb2Marshaller绑定单个类时,在嵌套类加载器下会无法识别实体类的XML映射注解,导致解析出的字段全部为空。
解决方法
- 先确认
Product实体类上已经添加了@XmlRootElement(name = "productRecord")以及对应字段的@XmlElement注解,注解的name属性和XML节点名完全一致(大小写敏感) - 将Jaxb2Marshaller的绑定规则从绑定单个类改为扫描指定包:
@Bean public StaxEventItemReader<Product> reader() { Jaxb2Marshaller productMarshaller = new Jaxb2Marshaller(); // 替换为你的Product类所在的包路径 productMarshaller.setPackagesToScan("com.yourbusiness.entity"); return new StaxEventItemReaderBuilder<Product>() .name("reader") .addFragmentRootElements("productRecord") .unmarshaller(productMarshaller) .strict(false) .build(); }
- 若仍未解决,可在
application.yml中添加配置指定JAXB上下文工厂:
spring: jaxb: context-factory: com.sun.xml.bind.v2.ContextFactory
内容的提问来源于stack exchange,提问作者langar
相关产品推荐
相关产品推荐

