1. 程式人生 > >SpringBoot學習(二)SpringBoot一些很實用的功能

SpringBoot學習(二)SpringBoot一些很實用的功能

一、定製Banner

springboot在啟動的時候,會有下面這個圖片
這裡寫圖片描述
然後這個圖案是可以自定義的,在src/main/resource下新建一個banner.txt檔案,然後去http://patorjk.com/software/taag/下自定義自己所要顯示的文字,然後點選下面的select & copy把複製的內容複製到banner.txt檔案中,重啟一下Springboot,圖示就會發生變化。
這裡寫圖片描述
當然這個圖案也是可以關閉的,在之前的版本,可以通過下面的方式關閉,但是在1.5.6,就是我現在學習的版本,這種方法是不可以的。


    public static void main
(String[] args) { // SpringApplication.run(Application.class, args); SpringApplication application = new SpringApplication(Application.class); application.setShowBanner(false); application.run(args); }

在SpringBoot1.5.6中,可以使用下面的方式關閉該圖案

    public static void main(String[] args) {
        new SpringApplicationBuilder(Demo2Application.class)
            .bannerMode(Mode.OFF)
            .run(args);
    }

或者

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Demo2Application.class);
        application.setBannerMode(Mode.OFF);
        application.run(args);
    }

同時可以在banner.txt這個檔案中新增下面這些東西顯示當前的使用的SpringBoot的版本號

    ${spring-boot.version}           #1.5.6.RELEASE    
    ${spring-boot.formatted-version} # (v1.5.6.RELEASE)

二、application.yml

在新的檔案下,會有一個application.properties,這大概是Springboot中唯一的一個配置檔案了吧。這個檔案預設的是空的。
application.properties檔案中新增下面的資訊

server.port=8888
server.context-path=/demo_2

這裡主要是修改server的埠號和路徑。

還有一種是application.yml檔案,把原來的檔案刪除,或者移動到其他地方,然後在根目錄(同級目錄src/main/resources)下新增application.yml,在yml中按照下面這麼配置。

server:
  port: 8443
  context-path: /demo_2

這個配置和properties實現的是一個功能,但是從檔案上看,yml檔案似乎更簡潔,而且不管是eclipse還是idea中都支援對yml檔案提示和自動補全,所以現在使用yml檔案居多,所以接下來我都是使用yml檔案配置的。

三、配置logback

在springBoot支援大部分日誌配置,包括slf,log4j等日誌框架,預設是使用logback,其他的日誌框架百度一下都是有的,這裡不做記錄,主要就講一下logback的配置

logging:
  file: logfile/log.log
  level: org.springframework.web = DEBUG

配置一下上面的資訊一下,重啟一下服務,重新整理一下專案,就可以看到logfile/log.log檔案,裡面是日誌資訊。

四、熱部署

springboot本身是不支援熱部署的,每一次修改程式之後都需要重啟一下專案,這樣開發效率就會很低,所以熱部署還是很有必要的。

在pom.xml檔案,新增下面的配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <!--
    <optional>true</optional>
    optional=true,依賴不會傳遞,該專案依賴devtools;
    之後依賴myboot專案的專案如果想要使用devtools,需要重新引入 
    -->
</dependency>

然後springboot就支援熱部署了。每當發現class檔案被修改,專案就會被重新載入。

五、profile配置

有時候開發和上線用的配置檔案application.yml時不一樣的,但是修改yml有時候會很麻煩。可以用下面的方式進行配置

在src/main/resources檔案下新增下面兩個檔案,注意application-是不能改的,後面的可以自定義

application-dev.yml
application-pro.yml

然後在application.yml檔案中只寫入下面的東西就可以了。

spring:
  profiles:
    active:
      - pro

這樣就可以將application-pro.yml載入進專案。
在我這短期這個功能是用不上的感覺。

六、替換favicon

springboot中預設的logo是一個綠葉形狀的這裡寫圖片描述,但是在實際中這個logo常常是需要替換成自己公司或者專案的圖示的。

#下面是關閉圖示,但是失敗了,還是會有
spring:
  mvc:
    favicon:
      enabled: false

官方文件中說用這個方法可以去掉,但是1.5.6中,這種方法這種方向是不可以的。所以目前來說我還不知道怎麼去掉這個logo,但是可以替換。

將自己的圖案重新命名為favicon.ico,然後放在static資料夾中。
在springboot中static是用來放資源的,templates是用來放頁面的。
然後在每個頁面中新增

<link rel="icon" type="image/x-icon" href="favicon.ico" />

這樣就可以把logo替換掉,但是畢竟這種方式還是不是很方便,但是目前我就知道這種。

七、starter pom

名稱 描述
spring-boot-starter 核心Spring Boot starter,包括自動配置支援,日誌和YAML
spring-boot-starter-actuator 生產準備的特性,用於幫你監控和管理應用
spring-boot-starter-amqp 對”高階訊息佇列協議”的支援,通過spring-rabbit實現
spring-boot-starter-aop 對面向切面程式設計的支援,包括spring-aop和AspectJ
spring-boot-starter-batch 對Spring Batch的支援,包括HSQLDB資料庫
spring-boot-starter-cloud-connectors 對Spring Cloud Connectors的支援,簡化在雲平臺下(例如,Cloud Foundry 和Heroku)服務的連線
spring-boot-starter-data-elasticsearch 對Elasticsearch搜尋和分析引擎的支援,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire 對GemFire分散式資料儲存的支援,包括spring-data-gemfire
spring-boot-starter-data-jpa 對”Java持久化API”的支援,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb 對MongoDB NOSQL資料庫的支援,包括spring-data-mongodb
spring-boot-starter-data-rest 對通過REST暴露Spring Data倉庫的支援,通過spring-data-rest-webmvc實現
spring-boot-starter-data-solr 對Apache Solr搜尋平臺的支援,包括spring-data-solr
spring-boot-starter-freemarker 對FreeMarker模板引擎的支援
spring-boot-starter-groovy-templates 對Groovy模板引擎的支援
spring-boot-starter-hateoas 對基於HATEOAS的RESTful服務的支援,通過spring-hateoas實現
spring-boot-starter-hornetq 對”Java訊息服務API”的支援,通過HornetQ實現
spring-boot-starter-integration 對普通spring-integration模組的支援
spring-boot-starter-jdbc 對JDBC資料庫的支援
spring-boot-starter-jersey 對Jersey RESTful Web服務框架的支援
spring-boot-starter-jta-atomikos 對JTA分散式事務的支援,通過Atomikos實現
spring-boot-starter-jta-bitronix 對JTA分散式事務的支援,通過Bitronix實現
spring-boot-starter-mail 對javax.mail的支援
spring-boot-starter-mobile 對spring-mobile的支援
spring-boot-starter-mustache 對Mustache模板引擎的支援
spring-boot-starter-redis 對REDIS鍵值資料儲存的支援,包括spring-redis
spring-boot-starter-security 對spring-security的支援
spring-boot-starter-social-facebook 對spring-social-facebook的支援
spring-boot-starter-social-linkedin 對spring-social-linkedin的支援
spring-boot-starter-social-twitter 對spring-social-twitter的支援
spring-boot-starter-test 對常用測試依賴的支援,包括JUnit, Hamcrest和Mockito,還有spring-test模組
spring-boot-starter-thymeleaf 對Thymeleaf模板引擎的支援,包括和Spring的整合
spring-boot-starter-velocity 對Velocity模板引擎的支援
spring-boot-starter-web 對全棧web開發的支援,包括Tomcat和spring-webmvc
spring-boot-starter-websocket 對WebSocket開發的支援
spring-boot-starter-ws 對Spring Web服務的支援
spring-boot-starter-actuator 新增生產準備特性,比如指標和監控
spring-boot-starter-remote-shell 新增遠端ssh shell支援
spring-boot-starter-jetty 匯入Jetty HTTP引擎(作為Tomcat的替代)
spring-boot-starter-log4j 對Log4J日誌系統的支援
spring-boot-starter-logging 匯入Spring Boot的預設日誌系統(Logback)
spring-boot-starter-tomcat 匯入Spring Boot的預設HTTP引擎(Tomcat)
spring-boot-starter-undertow 匯入Undertow HTTP引擎(作為Tomcat的替代)