@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
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
配置文件中${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)}
java -jar xx.jar --server.port=9090
application.properties和application.yml文件能夠放在一下四個位置:
一樣,這個列表按照優先級排序,也就是說,src/main/resources/config下application.properties覆蓋src/main/resources下application.properties中相同的屬性,如圖:
此外,若是你在相同優先級位置同時有application.properties和application.yml,那麼application.yml裏面的屬性就會覆蓋application.properties裏的屬性。
Profile是Spring用來針對不一樣環境提供不一樣配置的支持,全局Profile配置使用application-{profile}.properties。經過application.properties中設置spring.profiles.active值
在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
application.properties中配置默認值:
spring: profiles: active: trunk
http://blog.javachen.com/2016...
http://blog.csdn.net/lihe2008...