Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从资源目录获取单元测试的文件,但获取产品的绝对路径_Java_Spring Boot - Fatal编程技术网

Java 从资源目录获取单元测试的文件,但获取产品的绝对路径

Java 从资源目录获取单元测试的文件,但获取产品的绝对路径,java,spring-boot,Java,Spring Boot,我有一个springboot命令行应用程序,其中一个生产命令行参数是绝对基本路径。对于这个例子,我们称之为 “/var/batch/” 我在production.yml文件中使用默认值设置basepath 公司: 基本路径:${basePath:/var/default/} 然后,我有一个ApplicationConfig.java文件,它使用该基本路径创建一组文件路径,如下所示 @ConfigurationProperties(prefix = "company") public class

我有一个springboot命令行应用程序,其中一个生产命令行参数是绝对基本路径。对于这个例子,我们称之为

“/var/batch/”

我在production.yml文件中使用默认值设置basepath

公司: 基本路径:${basePath:/var/default/}

然后,我有一个ApplicationConfig.java文件,它使用该基本路径创建一组文件路径,如下所示

@ConfigurationProperties(prefix = "company")
public class ApplicationConfig {

    private String basePath;

    public String getPrimaryCarePath() {
        return basePath + "ADAP-2-PCProv.dat";
    }

    public String getPrimaryCareDetailPath() {
        return basePath + "ADAP-2-" + getBatchNo() + ".det";
    }

    ... additional files. 
public List<T> readCsv() throws IOException {

    try (BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(filePath))) {
        return new CsvToBeanBuilder(bufferedReader)
                .withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS)
                .withType(subClass)
                .withSeparator('\t')
                .withIgnoreLeadingWhiteSpace(true)
                .build().parse();
    }
}
}

最后,文件路径像这样被传递到我的css解析器中

@ConfigurationProperties(prefix = "company")
public class ApplicationConfig {

    private String basePath;

    public String getPrimaryCarePath() {
        return basePath + "ADAP-2-PCProv.dat";
    }

    public String getPrimaryCareDetailPath() {
        return basePath + "ADAP-2-" + getBatchNo() + ".det";
    }

    ... additional files. 
public List<T> readCsv() throws IOException {

    try (BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(filePath))) {
        return new CsvToBeanBuilder(bufferedReader)
                .withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS)
                .withType(subClass)
                .withSeparator('\t')
                .withIgnoreLeadingWhiteSpace(true)
                .build().parse();
    }
}

我们所有的测试文件都存储在测试资源包中,所以我的问题是,我们如何使用相对路径来填充ApplicationConfig.java文件中的测试资源,同时仍然能够使用绝对路径进行生产?我想我可以使用ClassPathResource用测试设置覆盖basepath,但不知道是否有更好的方法

您需要两种类型的配置:一种用于资源,另一种用于绝对路径

我建议添加一个新属性
app.file.path.type
,其值为
resources
absolute
。您可以定义名为
FileProvider
的新接口

public interface FilePathProvider(){
    Path getFilePath();
}
您可以使用
@conditionalnproperty
定义两个不同的bean,并设置文件路径策略:

@Configuration
public class ApplicationConfig{

      @Bean
      @ConditionalOnProperty(
            name = "app.file.path.type", 
            havingValue = "absolute")
      public FilePathProvider absoluteFilePathProvider(ApplicationConfig applicationConfig){
           return () -> Paths.get(applicationConfig.getBasePath());
      }

     @ConditionalOnProperty(
            name = "app.file.path.type", 
            havingValue = "resources")
      @Bean
      public FilePathProvider resourceFilePathProvider(ApplicationConfig applicationConfig){
           return () -> Paths.get(this.getClass().getClassLoader().getResource(applicationConfig.getBasePath()).getPath());
      }
} 
在开发和测试模式下,您将拥有
app.file.path.type=resources
,在生产模式下,您将拥有
app.file.path.type=absolute

这种方法的优点是,在开发过程中也可以将属性设置为
absolute

我喜欢这种方法,但是我发现了一个小问题。如何让它使用相同的方法名/签名?我有编译错误。您有使用示例吗?直接在stackoverflow上编写代码不是那么容易:))很抱歉,您必须有不同的方法签名。最后一个问题是,如果方法签名不同,如何调用条件属性?您将不会调用
@ConditionalOnProperty
,spring将为您完成此任务。仅当基于输入属性的条件为true=>时,才会调用该方法FilePathProvider将具有不同的行为。您需要使用
@Autowired filepath provider filepath provider
在需要
基本路径的地方自动连接此bean,并调用
filepath provider.getFilePath()