3

I am making a directory to store all uploaded files in my spring boot app on startup.

The path of this directory is stored in application.properties file. I am trying to read this path and create a directory on startupof project. I am not able to get the path while creating a directory on startup.

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;
    }

}
Daniel
  • 10,641
  • 12
  • 47
  • 85
omkar
  • 55
  • 1
  • 6
  • [this answer](https://stackoverflow.com/questions/27405713/running-code-after-spring-boot-starts/44923402#44923402) may help. – lin May 17 '19 at 13:31
  • I recently did the same thing in my spring boot application, if you don't find a suitable solution let me know, I can show you how I did. – akuma8 May 17 '19 at 14:06
  • 1
    Make your storage properties as component and register an event ApplicationReady and write your business logic here to create the folder – DEBENDRA DHINDA May 18 '19 at 07:33

1 Answers1

3
  • Step1: make you StorageProperties a Component
  • Step2: autowire that component in your StartUpComponent
  • Step3: create your folder
@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
  }
}
Dirk Deyne
  • 6,048
  • 1
  • 15
  • 32