SpringBoot_總結_01_配置詳解

1、入口類和@SpringBootApplication

SpringBoot項目一般有一個名爲*Application的入口類,入口方法爲此類的main方法。java

 

1. @SpringBootApplication

@SpringBootApplication註解是一個組合註解,主要組合了一下註解:mysql

(1)@Configuration : 聲明當前類是一個配置類,至關於一個Spring配置的xml文件。意味着這個類裏可能有0個或者多個@Bean註解。spring

(2)@EnableAutoConfiguration : 讓SpringBoot根據類路徑中的jar包依賴爲當前項目進行自動配置。sql

(3)@ComponentScan:設置bean掃描的包。數據庫

@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) }) public @interface SpringBootApplication 

 

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration

 

所以,若不使用@SpringBoot註解,則能夠在入口類上直接使用這個三個註解。安全

SpringBoot會自動掃描入口類所在的同級包以及下級包裏的bean。微信

 

2.關閉特定的自動配置

經過@SpringBootApplication註解的參數exclude參數實現:app

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

 

3.定製Banner

在SpringBoot啓動時會有一個默認啓動圖案:微信公衆平臺

 

 (1)修改Banneride

在src/main/resources 下新建一個banner.txt

////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕機 永無BUG // ////////////////////////////////////////////////////////////////////
View Code

也能夠去 http://patorjk.com/software/taag/ 在線生成字符,而後將生成的字符複製到banner.txt文件中便可。

 

 (2)關閉Banner

在入口文件Application.java的main方法中,修改成:

SpringApplication application = new SpringApplication(Application.class); application.setBannerMode(Banner.Mode.OFF); application.run(args);
View Code

 

4.使用xml配置

SpringBoot提倡零配置,即無XML配置,但實際上有些特殊的配置須要使用XML配置。咱們能夠經過Spring提供的@ImportResource來加載XML配置

@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})

 

 2、基本配置

1.默認配置文件

SpringBoot默認的配置文件名稱爲 application.properties,

默認搜索路徑爲:

file:./ // 當前目錄下的/config子目錄,
file:./config/ // 當前目錄
classpath:/                 // classpath根路徑
classpath:/config/ //classpath下的/config目錄

加載順序按優先級排序的(列表中位置高的將覆蓋位置低的)。

注:Spring-boot配置文件的加載,

先在與jar同級下查找,

若是沒有就去同級的config下查找;

若是再沒有,就在jar包中去查找相應的配置文件,

若是再沒有,就去jar包中的config下去查找。當查找到對應配置片斷時,採用增量替換的方式來進行替換。

 

 

具體可見源碼 ConfigFileApplicationListener :

 

 

2.profile

Profile爲在不一樣環境下使用不一樣的配置提供了支持(如開發環境和生成環境下數據庫的配置)

 若啓用dev開發環境配置,則須要在application.properties配置文件中,配置如下屬性

spring.profiles.active=dev

則,SpringBoot除了加載application.properties配置文件外,還會加載開發環境的application-dev.properties配置文件,以下:

# Mysql 注意替換相應配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/smart-blog
spring.datasource.username=root spring.datasource.password=root

 

3.server

(1)端口

server.port=9090

 

(2)應用名

server.servlet.context-path=/weixin-service。

 

 

3、外部配置

1.命令行參數配置

SpringBoot能夠基於jar包運行,打成jar包的程序能夠直接經過下面的命令行運行,並修改Tomcat端口號:

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

 

2.常規屬性配置

即@Value方式

(1)在application.properties中增長屬性:

      book.author=wangyunfei       book.name=Spring.boot

 

(2)在須要使用屬性的類中,如入口類:

@RestController @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class Application { @Value("${book.author}") private String bookAuthor; @Value("${book.name}") private String bookName; @RequestMapping("/") public String index() { return "book author is:" +  bookAuthor+ "  and book name is:   " + bookName ; } public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
View Code

 

3.類型安全的配置

即基於properties

@Value須要將屬性一個一個的注入,當屬性較多時,會比較麻煩。這時就能夠經過 @ConfigurationProperties將properties 屬性和一個Bean及其屬性關聯,從而實現安全配置。

(1)application.properties配置文件

在application.yml上添加(這裏以yml格式爲例),:

#微信公衆平臺配置 weixin: qy: corpId: ww92f5da92234696e agentSecret: I73733ve233s6H_ijPvIjYD4Rese5UlbYhhQOEE1-I contactsSecret: 1m_9XP62YrXjSiYtL5Th323rqaExKfr_5eAL09w agentId: 1000002 token: ray encodingAesKey: z2W9lyOAR1XjY8m2323qib0TlBZzCFiCLp6IdS2Iv state: hec4

咱們也能夠新建一個yml文件,這時咱們須要使用@PropertiesSource將添加的文件的位置指定。

 

(2)配置類

package com.ray.weixin.qy.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author : shira * @date : 2018/4/17 * @time : 22:33 * @desc : **/ @Data @Component @ConfigurationProperties(prefix = "weixin.qy") public class WeiXinAuthConfig { private String corpId; private String agentSecret; private String contactsSecret; private int agentId; private String token; private String encodingAesKey; private String state; }
View Code

 

經過 @ConfigurationProperties 加載文件內的配置,經過prefix屬性指定properties的配置的前綴。

 

而後在須要用的配置信息的時候,直接注入這個類就能夠了。

 

 

 

4、參考資料

1.《Java EE 開發的顛覆者—SpringBoot 實戰》,汪雲飛

相關文章
相關標籤/搜索