一、SpringBoot
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發程序,該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置,
二、SpringBoot啟動原理
SpringBoot整個啟動流程分為兩個步驟:
1、初始化一個SpringApplication物件
2、執行該物件的run方法,
SpringApplication初始化
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = deduceWebApplicationType();
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
SpringApplication.run方法
public ConfigurableApplicationContext run(String... args) {
//計時
StopWatch stopWatch = new StopWatch();
stopWatch.start();
//spring容器
ConfigurableApplicationContext context = null;
//錯誤回呼
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//設定一些系統屬性
configureHeadlessProperty();
//獲取啟動時監聽器
SpringApplicationRunListeners listeners = getRunListeners(args);
//啟動監聽器
listeners.starting();
try {
//獲取一些啟動引數
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//創建運行環境environment
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
//設定一些系統引數
configureIgnoreBeanInfo(environment);
//列印banner
Banner printedBanner = printBanner(environment);
//創建spring容器
context = createApplicationContext();
//獲取例外報告,回呼
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//準備容器
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//重繪容器
refreshContext(context);
//spring容器后置處理
afterRefresh(context, applicationArguments);
//計時終止
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
//結束通知
listeners.started(context);
//執行runner
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//spring容器就緒通知
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
//回傳容器
return context;
}
三、實作一個簡單SpringBoot專案(圖片上傳)
1、頁面代碼
一段簡單的JSP頁面代碼,注: method=“post” enctype="multipart/form-data"是必須要加的,不然會報錯,
<div>
<form action="/upload" method="post" enctype="multipart/form-data">
請選擇上傳的圖片或檔案:<input type="file" name="file"/>
<input type="submit" value="上傳"/>
</form>
<%--<img src="pic/${fileName}"/>--%>
<img src="${filename}"/>
</div>
2、控制層
@RequestMapping(value = "upload")
public ModelAndView upload(@RequestParam(value = "file") MultipartFile file){
ModelAndView modelAndView = new ModelAndView();
if (file.isEmpty()) {
System.out.println("檔案為空");
}
String fileName = file.getOriginalFilename(); // 檔案名
String filePath = "F:\\Workspase\\BackController\\src\\main\\webapp\\jsp\\pic\\"; // 上傳后的路徑
//fileName = UUID.randomUUID() + suffixName; // 存庫的時候使用
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
modelAndView.addObject("filename","jsp/pic/"+fileName);
modelAndView.setViewName("dong");
return modelAndView;
}
ps:針對圖片無法回顯問題,首先檢查一下路徑寫的對不對!!! 這個是最重要的,如果路徑沒問題,可能是攔截器的問題,在application.properties中加上靜態資源訪問路徑就可以了,
spring.resources.static-locations=/jsp/pic/**
xDroid——讓安卓應用運行在Linux平臺上
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/280675.html
標籤:java
