一。 开发工具
springboot包含了一些开发工具集 只需要引用 spring-boot-devtools 依赖 开发工具在开发阶段默认开启 在被打包的程序 比如jar包 通过 java -jar运行
自动禁用 开发工具集
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- </dependency>
- </dependencies>
在spring.properties添加启动配置 (添加了maven依赖 该参数默认为true 如果想禁用 改成false)
- spring.devtools.restart.enabled=true
springboot可以定制哪些资源会自动重启 哪些资源不会自动重启 (静态资源如图片等)
spring-boot-devtools-1.5.7.RELEASE.jar下DevToolsSettings中定义了开发工具的逻辑
- public static final String SETTINGS_RESOURCE_LOCATION = "META-INF/spring-devtools.properties";
具体参考https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#using-boot-devtools
二。 自定义Banner
springboot默认启动时 输出的图标就是banner 比如
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v1.5.7.RELEASE)
spring提供了自定义banner的方式在 src/main/resources添加文件banner.txt 添加特殊的自定义符号 在该txt中可以使用资源文件的一些特殊信息
参考(https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#boot-features-banner)
比如我的banner.txt为
- ╭⌒╮¤ ` ${application.version}
-
- ╭╭ ⌒╮ ●╭○╮ ${spring-boot.version}
-
- ╰ ----╯/█∨█\
-
- ~~~~~~~~~~∏~~∏~~~~~~~~~~~
运行后显示的效果为
- 17:08:42.628 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
- 17:08:42.644 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/.*.txt, /spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
- 17:08:42.644 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/C:/Users/jiaozi/Documents/workspace-sts-3.9.0.RELEASE/sb/target/classes/, file:/C:/Users/jiaozi/Documents/workspace-sts-3.9.0.RELEASE/sbbean/target/classes/]
- ╭⌒╮¤ ` 1290
-
- ╭╭ ⌒╮ ●╭○╮ 1.5.7.RELEASE
-
- ╰ ----╯/█∨█\
-
- ~~~~~~~~~~∏~~∏~~~~~~~~~~~
三。随机值
springboot可以在properties文件中使用表达式产生一些随机值 比如
- my.secret=${random.value}
- my.number=${random.int}
- my.bignumber=${random.long}
- my.uuid=${random.uuid}
- my.number.less.than.ten=${random.int(10)}
- my.number.in.range=${random.int[1024,65536]}
具体参考https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#boot-features-external-config-random-values
四。 Profiles
springboot允许使用 profile来定义不同环境下的不同配置 比如在开发环境下 使用mysql 正式环境下用oracle 如果直接修改配置 非常麻烦
可以在程序中预设好mysql和oracle的环境 通过不同的标志来标志 当启动程序 传入哪个标识就加载哪个配置 这就是profiles
以下举例 假设有个接口是Db 有两个实现类 Mysql和Oracle 分别给添加两个配置类 定义不同的profile
添加接口和实现类
- package cn.et.profile;
- /**
- * 定义接口
- * @author jiaozi
- *
- */
- public interface Db {
- String getName();
- }
- /**
- * 定义mysql实现类
- * @author jiaozi
- *
- */
- class Mysql implements Db{
- @Override
- public String getName() {
- return "mysql";
- }
-
- }
- /**
- * 定义oracle实现类
- * @author jiaozi
- *
- */
- class Oracle implements Db{
- @Override
- public String getName() {
- return "oracle";
- }
- }
分别定义两个profile分别是开发环境(实例化mysql的bean)和生产环境(实例化oracle的bean)
开发环境profile定义
- package cn.et.profile;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Profile;
-
- @Profile(value="dev")
- @Configuration
- public class DataSourceBeanDev {
- @Bean
- public Db testBean() {
- return new Mysql();
- }
- }
生产环境profile定义
- package cn.et.profile;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Profile;
-
- @Profile(value="product")
- @Configuration
- public class DataSourceBeanProduct {
- @Bean
- public Db testBean() {
- return new Oracle();
- }
- }
添加一个控制层的rest类
- package cn.et.controller;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RestController;
-
- import cn.et.profile.Db;
-
- @RestController
- public class TestController {
-
- @Autowired
- private Db db;
- @GetMapping("/q")
- public String query() {
- return db.toString()+"=="+db.getClass().getTypeName();
- }
- }
添加main方法
- package cn.et;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.annotation.PropertySource;
-
- @SpringBootApplication
- public class SbApplication {
-
- public static void main(String[] args) {
- SpringApplication sa=new SpringApplication();
- sa.run(SbApplication.class, args);
-
- }
- }
application.properties 添加当前启动激活的profile
- spring.profiles.active=product
启动后访问 http://localhost:8080/q 页面输出的是Oracle的实例
修改spring.profiles.active=dev 页面输出的Mysql的实例
打包的程序 可以通过 java -jar a.jar --spring.profiles.active=product 来传递参数覆盖spring.properties参数
springboot还可以添加一些额外的启动profile
- spring.profiles: dev #激动dev
- spring.profiles.include:#同时激活以下所有
- - devredis
- - devdb
- - devmongodb
五。 日志配置
springboot默认使用common-logging进行日志记录 日志分为以下几个级别
- FATAL 错误可能导致程序崩溃
- ERROR 一般为异常 程序继续运行
- WARN 警告信息 非错误
- INFO 程序正常运行记录信息
- DEBUG 调试信息
- TRACE 跟踪信息 级别最低
- 一般设置为 某个级别 大于该级别之上所有级别日志都输出 级别从低到高依次为:
- FATAL-DEBUG-INFO-WARN-ERROR -FATAL
springboot默认的级别是INFO
通过在spring.properties中 设置debug=true设置级别为debug trace=true设置级别为trace
其他的级别通过以下配置设置
可以将日志定位输出到文件 (以下两个配置只能一个生效 都配置了 file生效)
- logging.path=d:/logs #在该目录下生成 spring.log日志文件
- logging.file=my.log #当前项目下 生成该文件
其他的日志配置参考
- https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#boot-features-logging