springboot配置文件详解

一:springboot热启动

1:热启动插件依赖

< dependency >
            < groupId >org.springframework.boot</ groupId >
            < artifactId >spring-boot-devtools</ artifactId >
</ dependency >

该插件功能:是boot的一个热部署工具,当我们修改了(类、属性文件、页面等)时,会自动重新启动应用(由于其采用的双类加载器机制,这个启动会非常快,比手动重启快很多倍)


2启动入口程序的方式必须是runas----->springboot app

 注意:如果在pom文件添加依赖建议重新启动工程  

二:自定义属性配置

定义:在application.properties属性配置文件中定义属性名和属性值

springboot.pic.url=192.168.28.120


取值:在控制器中取值

@Value ( "${springboot.pic.url}" )
private  String URL;



三:默认属性配置

Springboot有很多默认配置参考springboot文档。

比如:编码对中文支持很友好,统一utf-8

spring.messages.encoding=UTF-8 # Message   bundles encoding.
server.tomcat.uri-encoding=UTF-8 #   Character encoding to use to decode the URI.
spring.freemarker.charset=UTF-8 #   Template encoding.
spring.http.encoding.charset=UTF-8 #   Charset of HTTP requests and responses. Added to the "Content-Type"

比如:tomcat端口号

server.port=8080 # Server HTTP port.

比如thymeleaf模板引擎的默认配置

sadf.png


四:修改默认配置

#pojo中有date类型怎么转string类型就靠这两个配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Chongqing

 

五:通过属性配置文件配置随机数

1:重新新建一个random.properties属性配置文件,如下图:

2018-03-02_133724.png

2:配置随机数信息如下:

# 随机字符串
com.springboot.value=${random.value}
# 随机int
com.springboot.number=${random.int}
# 随机long
com.springboot.bignumber=${random.long}
# 10以内的随机数
com.springboot.ten=${random.int(10)}
# 10-20的随机数
com.springboot.ten2twenty =${random.int[10,20]}

3:将属性注入到一个Pojo实体类,这种注法,你发现并没有使用@Value注解哦,怎么做到的呢?

package  com.springboot.pojo;
  
import  org.springframework.beans.factory.annotation.Value;
import    org.springframework.boot.context.properties.ConfigurationProperties;
import    org.springframework.context.annotation.PropertySource;
import  org.springframework.context.annotation.Scope;
import    org.springframework.stereotype.Component;
@Component
@PropertySource ( "classpath:config/random.properties" )
@ConfigurationProperties (prefix= "com.springboot" )
public  class  RandomProperties {
     //随机字符串
     private  String value;
     //随机int
     private  int  number;
     //随机long
     private  long  bignumber;
     //10以内的随机数
     private  int  ten;
     //10-20的随机数
     private  int  ten2twenty;
     public  String getValue() {
         return  value;
     }
     public  void  setValue(String value) {
         this .value = value;
     }
     public  int  getNumber() {
         return  number;
     }
     public  void  setNumber( int  number) {
         this .number = number;
     }
     public  long  getBignumber() {
         return  bignumber;
     }
     public  void  setBignumber( long  bignumber) {
         this .bignumber = bignumber;
     }
     public  int  getTen() {
         return  ten;
     }
     public  void  setTen( int  ten) {
         this .ten = ten;
     }
     public  int  getTen2twenty() {
         return  ten2twenty;
     }
     public  void  setTen2twenty( int  ten2twenty) {
         this .ten2twenty = ten2twenty;
     }
    
    
}



4:Springboot的单元测试随机数

package  com.springboot;
  
import  org.junit.Test;
import  org.junit.runner.RunWith;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.boot.test.context.SpringBootTest;
import  org.springframework.test.context.junit4.SpringRunner;
  
import  com.springboot.pojo.RandomProperties;
  
  
@RunWith (SpringRunner. class )
@SpringBootTest
public  class    Springboot001ApplicationTests {
  
     @Autowired
     private  RandomProperties properties;
    
    
     @Test
     public  void  contextLoads() {
         System.out.println(properties.getBignumber());
         System.out.println(properties.getNumber());
         System.out.println(properties.getTen());
         System.out.println(properties.getTen2twenty());
         System.out.println(properties.getValue());
        
     }
  
}


六:YAML instead of properties

无需导入相关jar因为在新建spring boot 项目时会自动引入snakeyaml,从而自动实现对yaml的支持

举例子:

environments:

dev:

url: http://dev.bar.com

name: Developer Setup

prod:

url: http://foo.bar.com

name: My Cool App

 

 

Would be transformed into these properties:

 

environments.dev.url=http://dev.bar.com

environments.dev.name=Developer Setup

environments.prod.url=http://foo.bar.com

environments.prod.name=My   Cool Ap


注意:

1:一定要注意冒号后一定要加空格,要不然就无法生效

2:大小写敏感

3:使用缩进表示层级关系

4:缩进时不允许使用Tab键,只允许使用空格。

6:缩进的空格数目不重要,只要相同层级的元素左侧对齐即可

Yaml高级用法:http://samchu.logdown.com/posts/290211-spring-boot-yaml-uses

 

 

扩展:通过命令行配置属性

命令:java -jar xxx.jar --server.port=8888

通过使用–-server.port属性来设置xxx.jar应用的端口为8888。

java -jar xxx.jar --server.port=8888命令,等价于我们在application.properties中添加属性server.port=8888


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值