二.spring-boot:基本配置

一.基本配置

1.1 springboot入口類@SpringBootApplication

@SpringBootApplication 是springboot的核心註解,它是一個組合註解:html

@Target(ElementType.TYPE)    
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration    
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

雖然定義使用了多個Annotation進行了原信息標註,但實際上重要的只有三個Annotation:java

@Configuration(@SpringBootConfiguration點開查看發現裏面仍是應用了@Configuration)
這裏的@Configuration對咱們來講不陌生,它就是JavaConfig形式的Spring Ioc容器的配置類使用的那個@Configuration,SpringBoot社區推薦使用基於JavaConfig的配置形式,因此,這裏的啓動類標註了@Configuration以後,自己其實也是一個IoC容器的配置類。spring

@EnableAutoConfiguration,你們是否還記得Spring框架提供的各類名字爲@Enable開頭的Annotation定義?好比@EnableScheduling、@EnableCaching、@EnableMBeanExport等,@EnableAutoConfiguration的理念和作事方式其實一脈相承,簡單歸納一下就是,藉助@Import的支持,收集和註冊特定場景相關的bean定義。springboot

@ComponentScan這個註解在Spring中很重要,它對應XML配置中的元素,@ComponentScan的功能其實就是自動掃描並加載符合條件的組件(好比@Component和@Repository等)或者bean定義,最終將這些bean定義加載到IoC容器中。app


1.2 定製Banner

  1. springboot在啓動的時候

clipboard.png

  1. src/main/resource下新建一個banner.txt文件
  2. 經過生成字符網站生成想要的字符如:lvmama

clipboard.png


1.3 springboot 配置文件

1.3.1 文件自定義屬性

spring Boot使用一個全局配置文件application.properties或application.yml,做用是對一些默認的配置進行修改,一個簡單的案例:框架

將Tomcat默認端口8080修改爲8088,默認的訪問路徑"/"改成"/lvmama"less

application.yml:dom

server:
  port: 8088
  context-path: /lvmama

application.propertiesspring-boot

server.port: 8088
server.context-path: /lvmama
  • 推薦使用application.yml,配合看起來更直觀,清晰。yml 文件在寫的時候層次感強,並且少寫了代碼。

1.3.1 隨機值配置文件

配置文件中${random} 能夠用來生成各類不一樣類型的隨機值,從而簡化了代碼生成的麻煩,例如 生成 int 值、long 值或者 string 字符串。網站

dudu.secret=${random.value}
dudu.number=${random.int}
dudu.bignumber=${random.long}
dudu.uuid=${random.uuid}
dudu.number.less.than.ten=${random.int(10)}

1.3.2 外部配置-命令行參數配置

java -jar xx.jar --server.port=9090

1.3.3 配置文件的優先級

application.properties和application.yml文件能夠放在一下四個位置:

  • 外置,在相對於應用程序運行目錄的/congfig子目錄裏。
  • 外置,在應用程序運行的目錄裏
  • 內置,在config包內
  • 內置,在Classpath根目錄

一樣,這個列表按照優先級排序,也就是說,src/main/resources/config下application.properties覆蓋src/main/resources下application.properties中相同的屬性,如圖:

clipboard.png

此外,若是你在相同優先級位置同時有application.properties和application.yml,那麼application.yml裏面的屬性就會覆蓋application.properties裏的屬性。

1.3.4 Profile-多環境配置

Profile是Spring用來針對不一樣環境提供不一樣配置的支持,全局Profile配置使用application-{profile}.properties。經過application.properties中設置spring.profiles.active值

  1. 在src/resources/下新建application-normal.yml和application-trunk.yml文件並分別配置

    application-normal.yml

    server:
      port: 8080
      context-path: /lvmama/normal

    application-trunk.yml

    server:
      port: 80
      context-path: /lvmama/trunk
  2. application.properties中配置默認值:

    spring:
      profiles:
        active: trunk
  3. 使用 mvn spring-boot:run -Drun.profiles=normal

clipboard.png

clipboard.png

拓展:

http://blog.javachen.com/2016...
http://blog.csdn.net/lihe2008...

相關文章
相關標籤/搜索