1:热启动插件依赖
1 2 3 4 | < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-devtools</ artifactId > </ dependency > |
该插件功能:是boot的一个热部署工具,当我们修改了(类、属性文件、页面等)时,会自动重新启动应用(由于其采用的双类加载器机制,这个启动会非常快,比手动重启快很多倍)
2:启动入口程序的方式必须是runas----->springboot app
注意:如果在pom文件添加依赖建议重新启动工程
定义:在application.properties属性配置文件中定义属性名和属性值
1 | springboot.pic.url=192.168.28.120 |
取值:在控制器中取值
1 2 | @Value ( "${springboot.pic.url}" ) private String URL; |
Springboot有很多默认配置参考springboot文档。
比如:编码对中文支持很友好,统一utf-8
1 2 3 4 | 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端口号
1 | server.port=8080 # Server HTTP port. |
比如thymeleaf模板引擎的默认配置
1 2 3 | #pojo中有date类型怎么转string类型就靠这两个配置 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=Asia/Chongqing |
1:重新新建一个random.properties属性配置文件,如下图:
2:配置随机数信息如下:
1 2 3 4 5 6 7 8 9 10 | # 随机字符串 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注解哦,怎么做到的呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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的单元测试随机数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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 |