springboot基础二:配置文件设置

概述

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

  1. 在项目resources文件夹下新建application.properties,另外也支持yml文件,可是咱们推荐使用properties这种配置文件git

  2. 在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配置这块应该就这么多内容,很简单吧,敲敲代码代码练习一下吧