spring-boot我想已經有很多人瞭解太的概念了吧,不瞭解的你可以網上一查一大堆,但是概念有的時候還是很難讓人深入理解,我知道它,但是它是幹什麼的就不知道了。
以下摘要是網上對比spring的,看客可以參考以下:
- Springboot之Spring與Spring boot的區別:一直以來,由於對SPRING的理解不深入。在使用中總是Spring boot的時候認爲它是SPRING的一部分,事實不然。
- Spring boot是一個在Spring 的基礎上搭建的全新的微框架,其目的是簡化Spring的搭建和開發過程。
- 有以下的特點:
- 1)簡單易用,初學者和大牛都可以輕鬆上手,其中的註解會給使用者提供方便;
- 2)Spring boot對第三方技術進行了很好的封裝和整合,提供了大量第三方接口;
- 3)可以通過依賴自動配置,不需要XML等配置文件
- 4)還提供了安全等特性姑且先不做理會。
我們今天主要是快速啓動一個spring-boot的項目,然後每天往裏面加點東西,一點一點的加進去,項目再不斷的完善,即可慢慢了解它到底有什麼好處,能給我們帶來哪些好處和便捷。
今天首先第一步,從頭搭建項目,你也可以直接從官網上下載一個,也可以通過maven自己創建,我這裏是通過我本地自己創建maven的
填寫好相關的信息,然後點擊finish完成即可
項目創建完之後,就需要往裏面添加spring-boot所需要的jar包了,首先找到項目的pom.xml文件,然後在裏面添加如下代碼:
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.7.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
添加完之後,接下來就是要設置啓動類了,在項目的src目錄下,創建一個com.lwl.boot.controller的包,然後創建MyApplication.java類:具體如下
- package com.lwl.boot.controller;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
- import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
- import org.springframework.boot.web.servlet.ErrorPage;
- import org.springframework.context.annotation.Bean;
- import org.springframework.http.HttpStatus;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- @EnableAutoConfiguration
- /**
- * @EnableAutoConfiguration 用於自動配置。簡單的說,它會根據你的pom配置(實際上應該是根據具體的依賴)來判斷這是一個什麼應用,並創建相應的環境。
- */
- public class MyApplication {
- @RequestMapping("/")
- @ResponseBody
- public String index() {
- return "你現在看到的是帥帥的我";
- }
- @RequestMapping("/hello")
- @ResponseBody
- public String hello() {
- return "hello 帥帥的我出現啦";
- }
- public static void main(String[] args) throws Exception {
- /**
- * SpringApplication 則是用於從main方法啓動Spring應用的類。默認,它會執行以下步驟:
- 1.創建一個合適的ApplicationContext實例 (取決於classpath)。
- 2.註冊一個CommandLinePropertySource,以便將命令行參數作爲Spring properties。
- 3.刷新application context,加載所有單例beans。
- 4.激活所有CommandLineRunner beans。
- 默認,直接使用SpringApplication 的靜態方法run()即可。但也可以創建實例,並自行配置需要的設置。
- */
- SpringApplication.run(MyApplication.class, args);
- //通過自定義參數設置,啓動
- // SpringApplication app = new SpringApplication(MyApplication.class);
- // app.run(args);
- /**
- * 如果設置 properties 則配置參數會讀取properties設置的值
- */
- }
- /**
- * 設置基本異常處理拋出頁面
- * @return
- * @create 2017-10-9 上午11:44:13
- */
- @Bean
- public EmbeddedServletContainerCustomizer containerCustomizer() {
- return new EmbeddedServletContainerCustomizer() {
- @Override
- public void customize(ConfigurableEmbeddedServletContainer container) {
- ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
- ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
- ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
- container.addErrorPages(error401Page, error404Page, error500Page);
- }
- };
- }
- }
右擊→啓動這個類即可加載項目
- 如果設置 properties 則配置參數會讀取properties設置的值
這個有個特別的配置文件,以後會慢慢詳解,先來看看項目運行起來的效果
打開瀏覽器,查看運行效果http://localhost:8090/hello
代碼已經上傳到我的github上面,如果有需要可以去下載:https://github.com/1181888200/spring-boot.git