1. 程式人生 > 實用技巧 >乙太網幀以及ARP,ICMP協議介紹

乙太網幀以及ARP,ICMP協議介紹

SpringBoot 學習筆記

一、SpringBoot 介紹

  SpringBoot 是由 Pivotal 團隊研發的,SpringBoot 並不是一門新技術,只是將之前常用的 Spring,SpringMVC,data-jpa 等框架封裝到了一起,只是幫助你隱藏這些框架的整合細節,實現敏捷開發。
  SpringBoot 就是一個工具集。
  SpringBoot 特點:

  • SpringBoot 專案不需要模板化的配置。
  • 在 SpringBoot 中整合第三方框架時,只需要匯入相應的 starter 依賴包,就自動整合了。
  • SpringBoot 預設只有一個 .properties 的配置檔案,不推薦使用 xml,且後期會採用 .java 的檔案去編寫配置資訊,減去了很多配置麻煩問題。
  • SpringBoot 工程在部署的時候,採用的是jar包的方式,內部自動依賴 Tomcat 容器,提供了多環境的配置。

二、SpringBoot 快速入門

  下面將介紹下怎麼快速建立 SpringBoot 專案。
  1. 如圖所示,依次點選 File -> New -> Project ,選擇 SpringBoot Initializr,新建一個 SpringBoot 專案,然後點 Next 。注意,新建過程中必須保持電腦聯網,因為建立專案時需要連線到外部地址 https://start.spring.io 。

  2. 如圖所示是新建專案的相關資訊介紹,資訊填寫好後直接點選 Next 。

  3. 手動選擇需要依賴的 jar 包,SpringBoot 用預設版本即可,點選 Next ,再次確認專案名,點選 Finish 即完成 SpringBoot 專案新建。注意,第一次建立 SpringBoot 工程,會下載大量依賴,需保證 Maven 已經配置了阿里雲私服,否則會很慢甚至會建立失敗。

三、SpringBoot 常用註解

  1. 啟動類 @SpringBootApplication
    SpringBoot 的專案一般都會有 XxxApplication 的入口類,入口類中會有 main 方法,這是一個標準的 Java 應用程式的入口方法。
    這個入口類都會有 @SpringBootApplication 註解,它讓 SpringBoot 自動給程式進行必要的配置,該註解是 SpringBoot 特有的。


    這個配置等同於以下幾個註解之和:
      @SpringBootConfiguration :表示 Application 作為配置檔案存在
      @EnableAutoConfiguration :表示啟用 SpringBoot 內建的自動配置功能
      @ComponentScan : 掃描 bean,路徑為 Application 類所在 package 以及 package 下的子路徑,在 Springboot 中 bean 都放置在該路徑以及子路徑下。

  2. 表現層 @Controller 等。

1 @RestController
2 public class TestController {
3     @RequestMapping("/test")
4     public String test(){
5         return "test!";
6    }
7 }

  @RestController 和 @RequestMapping 是 SpringMVC 的註解,不是 SpringBoot 特有的註解。

  (1) @RestController
    用於處理HTTP請求,@RestController = @Controller + @ResponseBody
  (2) @ResponseBody
    將 java 物件轉為 json 格式的資料。
    @ResponseBody 註解的作用是將 controller 的方法返回的物件通過適當的轉換器轉換為指定的格式之後,寫入到 response 物件的 body 區,通常用來返回 JSON 資料或者是 XML 資料。
注意:在使用此註解之後不會再走檢視處理器,而是直接將資料寫入到輸入流中,他的效果等同於通過 response 物件輸出指定格式的資料。
    @ResponseBody 表示該方法的返回結果直接寫入 HTTP response body 中,一般在非同步獲取資料時使用,如 AJAX。
  (3) @RequestMapping
    用於配置url對映
    @GetMapping 組合註解相當於 @RequestMapping(method = RequestMethod.GET)
    @PostMapping 組合註解相當於 @RequestMapping(method = RequestMethod.POST)
  (4) @RequestParam
    將請求引數繫結到你控制器的方法引數上,是 springmvc 中接收普通引數的註解。
    請求中的引數名和處理器中的形參名不一致時用 @RequestParam。
    語法:@RequestParam(value = "引數名", required = "true/false", defaultValue = "")
      value:引數名
      required:是否包含該引數,預設為 true,表示該請求路徑中必須包含該引數,如果不包含就報錯。
      defaultValue:預設引數值,如果設定了該值,required=true將失效,自動為false,如果沒有傳該引數,就使用預設值。

1 @GetMapping("/add")
2 public void addUser(@RequestParam("name")String name){
3 
4 }

  

  3. 業務層@Service 等。

  4. 持久層 @Repository 等。

  5. 任意層

  (1) @Component
    @Component, @Service, @Controller, @Repository 是 Spring 註解,註解後可以被 Spring 框架所掃描並注入到 Spring 容器來進行管理。
    @Component 是通用註解,其他三個註解是這個註解的拓展,並且具有了特定的功能。
  (2) 讀取配置檔案
    @Value:注入 Spring boot application.properties 配置的屬性的值。
  (3) 生成 Bean
    @Autowired:自動匯入依賴的 bean。
    @Resource:@Autowired 與 @Resource 都可以用來裝配 bean,都可以寫在欄位上或寫在 setter 方法上。


四、SpringBoot常用配置項

  SpringBoot專案常用的配置項很全面,在 application.properties 中設定修改即可。格式:配置項 = 配置值,如:spring.thymeleaf.prefix = classpath:/templates/ 

  server

  • server.address指定 server 繫結的地址。
  • server.compression.enabled是否開啟壓縮,預設為 false。
  • server.compression.excluded-user-agents指定不壓縮的 user-agent,多個以逗號分隔,預設值為: text/html,text/xml,text/plain,text/css
  • server.compression.mime-types指定要壓縮的MIME type,多個以逗號分隔.
  • server.compression.min-response-size執行壓縮的閾值,預設為 2048。
  • server.context-parameters.[param name]設定 servlet context 引數。
  • server.context-path設定應用的 context-path。(如設定context-path=server.context-path,所以訪問的路徑字首都要有/springboot。)
  • server.display-name設定應用的展示名稱,預設: application。
  • server.jsp-servlet.class-name設定編譯 JSP 用的 servlet,預設: org.apache.jasper.servlet.JspServlet)。
  • server.jsp-servlet.init-parameters.[param name]設定 JSP servlet 初始化引數。
  • server.jsp-servlet.registered設定 JSP servlet 是否註冊到內嵌的 servlet 容器,預設 true。
  • server.port設定 http 監聽埠。
  • server.servlet-path設定 dispatcher servlet 的監聽路徑,預設為: /。

  mvc

  • spring.mvc.async.request-timeout設定async請求的超時時間,以毫秒為單位,如果沒有設定的話,以具體實現的超時時間為準,比如 tomcat 的 servlet3 的話是 10 秒。

  • spring.mvc.date-format設定日期的格式,比如 dd/MM/yyyy。

  • spring.mvc.favicon.enabled是否支援 favicon.ico,預設為: true。

  • spring.mvc.ignore-default-model-on-redirect在重定向時是否忽略預設 model 的內容,預設為 true。

  • spring.mvc.locale指定使用的 Locale。

  • spring.mvc.message-codes-resolver-format指定 message codes 的格式化策略(PREFIX_ERROR_CODE,POSTFIX_ERROR_CODE)。

  • spring.mvc.view.prefix指定 mvc 檢視的字首。

  • spring.mvc.view.suffix:指定 mvc 檢視的字尾。

  messages

  • spring.messages.basename:指定 message 的 basename,多個以逗號分隔,如果不加包名的話,預設從 classpath 路徑開始,預設: messages。

  • spring.messages.cache-seconds:設定載入的資原始檔快取失效時間,-1 的話為永不過期,預設為 -1。

  • spring.messages.encoding:設定 Message bundles 的編碼,預設:UTF-8。

  mobile

  • spring.mobile.devicedelegatingviewresolver.enable-fallback:是否支援fallback的解決方案,預設false

  • spring.mobile.devicedelegatingviewresolver.enabled:是否開始device view resolver,預設為: false

  • spring.mobile.devicedelegatingviewresolver.mobile-prefix:設定mobile端檢視的字首,預設為:mobile/

  • spring.mobile.devicedelegatingviewresolver.mobile-suffix:設定mobile檢視的字尾

  • spring.mobile.devicedelegatingviewresolver.normal-prefix:設定普通裝置的檢視字首

  • spring.mobile.devicedelegatingviewresolver.normal-suffix:設定普通裝置檢視的字尾

  • spring.mobile.devicedelegatingviewresolver.tablet-prefix:設定平板裝置檢視字首,預設:tablet/

  • spring.mobile.devicedelegatingviewresolver.tablet-suffix:設定平板裝置檢視字尾.

  • spring.mobile.sitepreference.enabled:是否啟用SitePreferenceHandler,預設為: true

  view

  • spring.view.prefix:設定mvc檢視的字首.

  • spring.view.suffix:設定mvc檢視的字尾.

  resource

  • spring.resources.add-mappings是否開啟預設的資源處理,預設為true

  • spring.resources.cache-period設定資源的快取時效,以秒為單位.

  • spring.resources.chain.cache是否開啟快取,預設為: true

  • spring.resources.chain.enabled是否開啟資源 handling chain,預設為false

  • spring.resources.chain.html-application-cache是否開啟h5應用的cache manifest重寫,預設為: false

  • spring.resources.chain.strategy.content.enabled是否開啟內容版本策略,預設為false

  • spring.resources.chain.strategy.content.paths指定要應用的版本的路徑,多個以逗號分隔,預設為:[/**]

  • spring.resources.chain.strategy.fixed.enabled是否開啟固定的版本策略,預設為false

  • spring.resources.chain.strategy.fixed.paths指定要應用版本策略的路徑,多個以逗號分隔

  • spring.resources.chain.strategy.fixed.version指定版本策略使用的版本號

  • spring.resources.static-locations指定靜態資源路徑,預設為classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/

  multipart

  • multipart.enabled是否開啟檔案上傳支援,預設為true

  • multipart.file-size-threshold設定檔案寫入磁碟的閾值,單位為MB或KB,預設為0

  • multipart.location指定檔案上傳路徑.

  • multipart.max-file-size指定檔案大小最大值,預設1MB

  • multipart.max-request-size指定每次請求的最大值,預設為10MB

  freemarker

  • spring.freemarker.allow-request-override指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項

  • spring.freemarker.allow-session-override指定HttpSession的屬性是否可以覆蓋controller的model的同名項

  • spring.freemarker.cache是否開啟template caching.

  • spring.freemarker.charset設定Template的編碼.

  • spring.freemarker.check-template-location是否檢查templates路徑是否存在.

  • spring.freemarker.content-type設定Content-Type.

  • spring.freemarker.enabled是否允許mvc使用freemarker.

  • spring.freemarker.expose-request-attributes設定所有request的屬性在merge到模板的時候,是否要都新增到model中.

  • spring.freemarker.expose-session-attributes設定所有HttpSession的屬性在merge到模板的時候,是否要都新增到model中.

  • spring.freemarker.expose-spring-macro-helpers設定是否以springMacroRequestContext的形式暴露RequestContext給Spring’s macro library使用

  • spring.freemarker.prefer-file-system-access是否優先從檔案系統載入template,以支援熱載入,預設為true

  • spring.freemarker.prefix設定freemarker模板的字首.

  • spring.freemarker.request-context-attribute指定RequestContext屬性的名.

  • spring.freemarker.settings設定FreeMarker keys.

  • spring.freemarker.suffix設定模板的字尾.

  • spring.freemarker.template-loader-path設定模板的載入路徑,多個以逗號分隔,預設: ["classpath:/templates/"]

  • spring.freemarker.view-names指定使用模板的檢視列表.

  velocity

  • spring.velocity.allow-request-override指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項

  • spring.velocity.allow-session-override指定HttpSession的屬性是否可以覆蓋controller的model的同名項

  • spring.velocity.cache是否開啟模板快取

  • spring.velocity.charset設定模板編碼

  • spring.velocity.check-template-location是否檢查模板路徑是否存在.

  • spring.velocity.content-type設定ContentType的值

  • spring.velocity.date-tool-attribute設定暴露給velocity上下文使用的DateTool的名

  • spring.velocity.enabled設定是否允許mvc使用velocity

  • spring.velocity.expose-request-attributes是否在merge模板的時候,將request屬性都新增到model中

  • spring.velocity.expose-session-attributes是否在merge模板的時候,將HttpSession屬性都新增到model中

  • spring.velocity.expose-spring-macro-helpers設定是否以springMacroRequestContext的名來暴露RequestContext給Spring’s macro類庫使用

  • spring.velocity.number-tool-attribute設定暴露給velocity上下文的NumberTool的名

  • spring.velocity.prefer-file-system-access是否優先從檔案系統載入模板以支援熱載入,預設為true

  • spring.velocity.prefix設定velocity模板的字首.

  • spring.velocity.properties設定velocity的額外屬性.

  • spring.velocity.request-context-attribute設定RequestContext attribute的名.

  • spring.velocity.resource-loader-path設定模板路徑,預設為: classpath:/templates/

  • spring.velocity.suffix設定velocity模板的字尾.

  • spring.velocity.toolbox-config-location設定Velocity Toolbox配置檔案的路徑,比如 /WEB-INF/toolbox.xml.

  • spring.velocity.view-names設定需要解析的檢視名稱.

  thymeleaf

  • spring.thymeleaf.cache是否開啟模板快取,預設true

  • spring.thymeleaf.check-template-location是否檢查模板路徑是否存在,預設true

  • spring.thymeleaf.content-type指定Content-Type,預設為: text/html

  • spring.thymeleaf.enabled是否允許MVC使用Thymeleaf,預設為: true

  • spring.thymeleaf.encoding指定模板的編碼,預設為: UTF-8

  • spring.thymeleaf.excluded-view-names指定不使用模板的檢視名稱,多個以逗號分隔.

  • spring.thymeleaf.mode指定模板的模式,具體檢視StandardTemplateModeHandlers,預設為: HTML5

  • spring.thymeleaf.prefix指定模板的字首,預設為:classpath:/templates/

  • spring.thymeleaf.suffix指定模板的字尾,預設為:.html

  • spring.thymeleaf.template-resolver-order指定模板的解析順序,預設為第一個.

  • spring.thymeleaf.view-names指定使用模板的檢視名,多個以逗號分隔.

  mustache

  • spring.mustache.cache是否Enable template caching.

  • spring.mustache.charset指定Template的編碼.

  • spring.mustache.check-template-location是否檢查預設的路徑是否存在.

  • spring.mustache.content-type指定Content-Type.

  • spring.mustache.enabled是否開啟mustcache的模板支援.

  • spring.mustache.prefix指定模板的字首,預設: classpath:/templates/

  • spring.mustache.suffix指定模板的字尾,預設: .html

  • spring.mustache.view-names指定要使用模板的檢視名.

  groovy

  • spring.groovy.template.allow-request-override指定HttpServletRequest的屬性是否可以覆蓋controller的model的同名項

  • spring.groovy.template.allow-session-override指定HttpSession的屬性是否可以覆蓋controller的model的同名項

  • spring.groovy.template.cache是否開啟模板快取.

  • spring.groovy.template.charset指定Template編碼.

  • spring.groovy.template.check-template-location是否檢查模板的路徑是否存在.

  • spring.groovy.template.configuration.auto-escape是否在渲染模板時自動排查model的變數,預設為: false

  • spring.groovy.template.configuration.auto-indent是否在渲染模板時自動縮排,預設為false

  • spring.groovy.template.configuration.auto-indent-string如果自動縮排啟用的話,是使用SPACES還是TAB,預設為: SPACES

  • spring.groovy.template.configuration.auto-new-line渲染模板時是否要輸出換行,預設為false

  • spring.groovy.template.configuration.base-template-class指定template base class.

  • spring.groovy.template.configuration.cache-templates是否要快取模板,預設為true

  • spring.groovy.template.configuration.declaration-encoding在寫入declaration header時使用的編碼

  • spring.groovy.template.configuration.expand-empty-elements是使用<br/>這種形式,還是<br></br>這種展開模式,預設為: false)

  • spring.groovy.template.configuration.locale指定template locale.

  • spring.groovy.template.configuration.new-line-string當啟用自動換行時,換行的輸出,預設為系統的line.separator屬性的值

  • spring.groovy.template.configuration.resource-loader-path指定groovy的模板路徑,預設為classpath:/templates/

  • spring.groovy.template.configuration.use-double-quotes指定屬性要使用雙引號還是單引號,預設為false

  • spring.groovy.template.content-type指定Content-Type.

  • spring.groovy.template.enabled是否開啟groovy模板的支援.

  • spring.groovy.template.expose-request-attributes設定所有request的屬性在merge到模板的時候,是否要都新增到model中.

  • spring.groovy.template.expose-session-attributes設定所有request的屬性在merge到模板的時候,是否要都新增到model中.

  • spring.groovy.template.expose-spring-macro-helpers設定是否以springMacroRequestContext的形式暴露RequestContext給Spring’s macro library使用

  • spring.groovy.template.prefix指定模板的字首.

  • spring.groovy.template.request-context-attribute指定RequestContext屬性的名.

  • spring.groovy.template.resource-loader-path指定模板的路徑,預設為: classpath:/templates/

  • spring.groovy.template.suffix指定模板的字尾

  • spring.groovy.template.view-names指定要使用模板的檢視名稱.

  http

  • spring.hateoas.apply-to-primary-object-mapper設定是否對object mapper也支援HATEOAS,預設為: true

  • spring.http.converters.preferred-json-mapper是否優先使用JSON mapper來轉換.

  • spring.http.encoding.charset指定http請求和相應的Charset,預設: UTF-8

  • spring.http.encoding.enabled是否開啟http的編碼支援,預設為true

  • spring.http.encoding.force是否強制對http請求和響應進行編碼,預設為true

  jackson

  • spring.jackson.date-format指定日期格式,比如yyyy-MM-dd HH:mm:ss,或者具體的格式化類的全限定名

  • spring.jackson.deserialization是否開啟Jackson的反序列化

  • spring.jackson.generator是否開啟json的generators.

  • spring.jackson.joda-date-time-format指定Joda date/time的格式,比如yyyy-MM-dd HH:mm:ss). 如果沒有配置的話,dateformat會作為backup

  • spring.jackson.locale指定json使用的Locale.

  • spring.jackson.mapper是否開啟Jackson通用的特性.

  • spring.jackson.parser是否開啟jackson的parser特性.

  • spring.jackson.property-naming-strategy指定PropertyNamingStrategy (CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)或者指定PropertyNamingStrategy子類的全限定類名.

  • spring.jackson.serialization是否開啟jackson的序列化.

  • spring.jackson.serialization-inclusion指定序列化時屬性的inclusion方式,具體檢視JsonInclude.Include列舉.

  • spring.jackson.time-zone指定日期格式化時區,比如America/Los_Angeles或者GMT+10.

  jersey

  • spring.jersey.filter.order指定Jersey filter的order,預設為: 0
  • spring.jersey.init:指定傳遞給Jersey的初始化引數.
  • spring.jersey.type:指定Jersey的整合型別,可以是servlet或者filter.