springboot爲何開發效率那麼快,很大一部分是由於提供了自動化配置,相比以前的框架各類xml,各類properties文件,這種顯示方式的聲明是不須要的,採用默認配置就能夠最輕量化的跑起一個應用程序,這一篇就講講springboot的配置html
springboot配置文件默認讀取依賴包中resources目錄下的application.properties文件,這裏面配置了一些springboot的默認配置,以下:另外能夠隨時關注springboot官網文檔裏的默認配置項(http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)ios
===================================================================
# COMMON SPRING BOOT PROPERTIES
# This sample file is provided as a guideline. Do NOT copy it in its
# entirety to your own application. ^^^
# ===================================================================
# ----------------------------------------
# CORE PROPERTIES
# ----------------------------------------
# SPRING CONFIG (ConfigFileApplicationListener)
spring.config.name= # config file name (default to 'application')
spring.config.location= # location of config file
# PROFILES
spring.profiles= # comma list of active profiles
# APPLICATION SETTINGS (SpringApplication)
spring.main.sources=
spring.main.web-environment= # detect by default
spring.main.show-banner=true
spring.main....= # see class for all properties
# LOGGING
logging.path=/var/logs
logging.file=myapp.log
logging.config=
# IDENTITY (ContextIdApplicationContextInitializer)
spring.application.name=
spring.application.index=
其他略。。。。。。。。。。。。。。。。。。。。。。。。。。。。
springboot web程序默認啓用的端口是8080,若是咱們想更換端口改成8888怎麼辦呢?
其實很簡單,步驟以下:nginx
在項目resources文件夾下新建application.properties,另外也支持yml文件,可是咱們推薦使用properties這種配置文件git
在application.properties配置文件裏添加以下配置便可github
#更改springboot內嵌tomcat服務器端口
server.port=8888
除了在咱們本身新建的application.properties配置文件裏修改默認配置外,咱們還能夠添加自定義的配置,例若有這樣一個需求,咱們項目裏用的文件服務器是阿里的fastdfs,須要自定義fastdfs的配置,都以fdfs開頭web
#fastdfs tracker配置信息
fdfs.trackerServer=192.168.1.100:22122
#fastdfs storage內網組名
fdfs.intranetGruop=group1
#fastdfs nginx模塊訪問地址
fdfs.showPrefixUrl=192.168.9.100:80
咱們的fasterdfs模塊初始化連接的時候,須要讀取配置文件,咱們只須要在對應的配置信息類裏用註解@ConfigurationProperties(prefix = 「fdfs」)引入前綴爲fdfs的配置文件便可spring
/**
* FastdfsInfo配置信息類
* @author fqh
* @email fanqinghui100@126.com
* @date 2017/6/24 22:31
*/
@Component
@ConfigurationProperties(prefix = "fdfs")
public class FastdfsInfo {
private String trackerServer;
private String intranetGruop;
private String showPrefixUrl;
//set get方法略
@Override
public String toString() {
return "FastdfsInfo{" +
"trackerServer='" + trackerServer + '\'' +
", intranetGruop='" + intranetGruop + '\'' +
", showPrefixUrl='" + showPrefixUrl + '\'' +
'}';
}
}
注意:值得注意的是properties文件裏的配置值最好別用中文,由於springboot是以ios-8859讀取application .properties的,yml文件則不會出現中文亂碼問題數據庫
springboot不單從application.properties文件裏獲取配置,還能夠在程序裏進行額外的自定義配置
能夠在Application啓動類裏使用@ImportResource註解進行引入,例如個人支付項目願支付(https://github.com/JoeyFan/wish-pay.git)里加載了額外的spring配置文件,就用到了這個方式tomcat
/**
* Spring Boot應用啓動類
*/
@MapperScan("com.wish.pay.web.dao")
@ComponentScan("com.wish.pay")
@SpringBootApplication
@ImportResource("classpath:spring/spring-context-pay.xml")
public class WebApplication extends SpringBootServletInitializer {
/**
* 無 applicationContext.xml 和 web.xml, 靠下述方式進行配置:
* <p>
* 1. 掃描當前package下的class設置自動注入的Bean<br/>
* 2. 也支持用@Bean標註的類配置Bean <br/>
* 3. 根據classpath中的三方包Class及集中的application.properties條件配置三方包,如線程池 <br/>
* 4. 也支持用@Configuration標註的類配置三方包.
*/
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
通常公司裏項目都有開發環境,測試環境,預上線環境,跟生成環境,每一個環境裏的一些配置值都是不一樣的,例如數據庫之類的配置等。
springboot對多環境設置這塊可謂是特別簡單的,例如咱們設置2個環境,開發環境dev與測試環境test,除了默認的application.properties配置文件外只須要新建2個配置文件application-dev.properties與application-test.properties,而後新建的每一個配置文件裏設置各自的屬性,在默認的application.properties裏只須要添加一行便可springboot
#指定springboot默認的開發環境爲dev
spring.profiles.active=dev
能夠參考我github上願支付(https://github.com/JoeyFan/wish-pay.git)的配置
關於springboot配置這塊應該就這麼多內容,很簡單吧,敲敲代碼代碼練習一下吧