我正在创建一个目录,用于在启动时将所有上传的文件存储在我的 spring boot 应用程序中。
该目录的路径存储在application.properties 文件中。 我正在尝试读取此路径并在 startupof 项目上创建一个目录。在启动时创建目录时无法获取路径。
应用程序属性
upload.path = "/src/main/resources"
存储属性.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;
}
}
最佳答案
- 第一步:让你的 StorageProperties 成为一个组件
- 第 2 步:在您的 StartUpComponent 中 Autowiring 该组件
- 第 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
}
}
关于java - 如何在spring boot项目启动时创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56187233/