XML 檔案的鏈接作為命令列引數傳遞給應用程式。鏈接格式如下:type:path,其中type是鏈接的型別,path是檔案的路徑。該鏈接定義了以 XML 格式加載資料的來源。鏈接型別(type):file(外部檔案)、classpath(classpath中的檔案)、url(URL)。示例:檔案:input.xml,類路徑:input.xml,url:檔案:/input.xml。我怎樣才能收到檔案?我試過@Value,但它只能傳遞常量。
uj5u.com熱心網友回復:
實作 ApplicationRunner 以通過ApplicationArguments獲取第一個位置引數 。有多種方法可以定位資源,該示例使用 DefaultResourceLoader。
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
@SpringBootApplication
@NoArgsConstructor @ToString @Log4j2
public class Command implements ApplicationRunner {
public static void main(String[] argv) throws Exception {
new SpringApplicationBuilder(Command.class).run(argv);
}
@Override
public void run(ApplicationArguments arguments) throws Exception {
/*
* Get the first optional argument.
*/
String link = arguments.getNonOptionArgs().get(0);
/*
* Get the resource.
*/
Resource resource = new DefaultResourceLoader().getResource(link);
/*
* Command implementation; command completes when this method
* completes.
*/
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/318590.html
