我正在创建一个目录,用于在启动时将所有上传的文件存储在我的spring boot应用程序中。
此目录的路径存储在application.properties文件中。 我正在尝试读取此路径并在startupof项目上创建目录。在启动时创建目录时无法获取路径。
upload.path = "/src/main/resources"
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;
}
}
答案 0 :(得分:0)
@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
}
}