Как создать каталог на стату в весеннем загрузочном проекте - PullRequest
0 голосов
/ 17 мая 2019

Я создаю каталог для хранения всех загруженных файлов в моем приложении весенней загрузки при запуске.

Путь к этому каталогу хранится в файле application.properties. Я пытаюсь прочитать этот путь и создать каталог при запуске проекта. Я не могу получить путь при создании каталога при запуске.

application.properties

upload.path = "/src/main/resources"

StorageProperties.java

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "upload")
public class StorageProperties {

    private String path;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

}

1 Ответ

0 голосов
/ 17 мая 2019
  • Шаг1: сделайте StorageProperties компонентом
  • Шаг 2: автоматически связать этот компонент в вашем StartUpComponent
  • Шаг 3: создайте папку
@Component
@ConfigurationProperties(prefix = "upload")
public class StorageProperties {

  private String path;

  // getters and setters
}
@Component
public class StartupComponent implements CommandLineRunner {
   private final StorageProperties storageProps;

   public StartupComponent (StorageProperties storageProps){
     this.storageProps = storageProps;
   }

  @Override
  public void run(String... args) throws Exception {
     String path = storageProps.getPath();
     // do your stuff here
  }
}
...