简体   繁体   English

Spring 引导批处理 MultiResourceItemReader:如何使用 @Value() 从 jar 位置读取文件

[英]Spring boot batch MultiResourceItemReader: how to read files using @Value() from jar location

I am trying to get the input files using @Value annotation and set it as a Resource array for MultiResourceItemReader as bellow;我正在尝试使用 @Value 注释获取输入文件并将其设置为 MultiResourceItemReader 的资源数组,如下所示;

@Value("classpath*:/input/userdata*.csv")
private Resource[] inputFiles;

It is working fine in IDE when I place input files folder in resources folder.当我将输入文件夹放在资源文件夹中时,它在 IDE 中工作正常。 The issues is when I run the jar file.问题是当我运行 jar 文件时。 It is also working fine if the files are within the jar as in IDE.如果文件在 jar 中(如 IDE 中),它也可以正常工作。 But it is not picking up when I place the input folder in the same directory of jar file.但是当我将输入文件夹放在 jar 文件的同一目录中时,它并没有出现。 Here is my Configuration;这是我的配置;

        @Value("classpath*:/input/userdata*.csv")
        private Resource[] inputFiles;
        
        @Bean
        public MultiResourceItemReader<UserInfo> multiResourceItemreader() {
            MultiResourceItemReader<UserInfo> reader = new MultiResourceItemReader<>();
            reader.setDelegate(userDataItemReader());
            reader.setResources(inputFiles);
            return reader;
        }

It did not give any error but just the bellow message in console它没有给出任何错误,只是控制台中的以下消息

2021-02-04 16:31:42.959  WARN 15772 --- [           main] o.s.b.item.file.MultiResourceItemReader  : No resources to read. Set strict=true if this should be an error condition.

I am not sure what I am missing here.我不确定我在这里缺少什么。 Tried without classpath prefix in @Value but it gives me bellow exception尝试在 @Value 中不使用类路径前缀,但它给了我以下异常

java.lang.IllegalArgumentException: Could not resolve resource location pattern [/input/userdata*.csv]

Not sure is there any other way to set the resources for MultiResourceItemReader other than @Value annotation.不确定除了 @Value 注释之外还有其他方法可以为 MultiResourceItemReader 设置资源。 A help would be greatly appreciated!非常感谢您的帮助!

Edit:- This is a Spring Boot batch application and here is the directory structure编辑:-这是一个 Spring 引导批处理应用程序,这里是目录结构

            apps
            ├── input
            │   ├── userdata6.csv
            │   ├── userdata7.csv
            │   ├── userdata8.csv
            │   └── userdata9.csv
            └── multisource-batch.jar

I run the jar file as below from the same app directory我从同一个应用程序目录运行 jar 文件如下

java -jar multisource-batch.jar

build.gradle build.gradle

        plugins {
          id 'org.springframework.boot' version '2.4.2'
          id 'io.spring.dependency-management' version '1.0.11.RELEASE'
          id 'java'
        }

        group = 'com.thomsoncodes.batch'
        version = '0.0.1-SNAPSHOT'
        sourceCompatibility = '1.8'

        repositories {
            mavenCentral()
            maven { url 'https://repo.spring.io/milestone' }
        }

        ext {
            set('springCloudVersion', "2020.0.0")
        }

        dependencies {
            implementation 'org.springframework.boot:spring-boot-starter-batch'
            runtimeOnly 'mysql:mysql-connector-java'
            testImplementation 'org.springframework.boot:spring-boot-starter-test'
            testImplementation 'org.springframework.batch:spring-batch-test'
            
            implementation 'org.projectlombok:lombok:1.18.2'
        }

        dependencyManagement {
            imports {
                mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
            }
        }

        test {
            useJUnitPlatform()
        }

UPDATE:- Finally I got it working.. Even though I removed the Web dependency the spring was still considering this as web: Did a clean build and updated @Value("file./input/userdata*.csv") as Mahmoud Ben Hassine suggested!更新:-最后我得到了它的工作..即使我删除了 Web 依赖项 spring 仍然认为这是 web:“Benuser 输入数据*”(“@csv”)作为Mahmo./uddata*。哈辛建议!

it is not picking up when I place the input folder in the same directory of jar当我将输入文件夹放在 jar 的同一目录中时,它没有出现

If you are you running your app with java -jar myapp.jar where the input directory is in the same directory of myapp.jar , then you need to use the file: prefix and not the classpath: prefix.如果您使用java -jar myapp.jar运行您的应用程序,其中input目录与myapp.jar位于同一目录中,那么您需要使用file:前缀而不是classpath:前缀。

@Value("file:input/userdata*.csv") should work as it will resolve resources from the relative directory where you launched your JVM. @Value("file:input/userdata*.csv")应该可以工作,因为它将从您启动 JVM 的相对目录中解析资源。

EDIT: You can find a complete example here .编辑:你可以在这里找到一个完整的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 spring-batch 和 MultiResourceItemReader 读取文件夹中的所有文件? - How to read all files in a folder with spring-batch and MultiResourceItemReader? 在 spring 批处理中,我们如何将处理后的文件移动到另一个文件夹,我正在使用 MultiResourceItemReader 和块处理 - In spring batch how can we move the processed files to another folder, I am using MultiResourceItemReader and chunk processing 如何使用MultiResourceItemReader读取多个文件? - How to read multiple files with MultiResourceItemReader? Spring批处理MultiResourceItemReader-仅用于合并文件? - Spring batch MultiResourceItemReader - only used for combining files? 如何使用Windows批处理文件一个一启动Spring Boot jar文件? - How to start spring boot jar files one by one using windows batch file? Spring Boot批处理-MultiResourceItemReader:错误时移至下一个文件 - Spring Boot batch - MultiResourceItemReader : move to next file on error 在Spring Batch中解密后通过MultiResourceItemReader读取文件 - Reading Files thru MultiResourceItemReader after de-cryption in Spring Batch Java Spring 批处理 - MultiResourceItemReader 不会在每次作业运行时读取新文件 - Java Spring Batch - MultiResourceItemReader not reading in new files every job run 春季批处理:如何直接从UNIX服务器位置中创建的安装点读取文件? - Spring batch: How to read files from mount point created in UNIX server location directly? 无法从 spring boot .jar 读取文件 - Unable to read file from spring boot .jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM