Springboot的常用配置

来源:互联网 发布:淘宝放错类目降权 编辑:程序博客网 时间:2024/05/14 16:35

一。 开发工具

   springboot包含了一些开发工具集 只需要引用 spring-boot-devtools 依赖 开发工具在开发阶段默认开启 在被打包的程序 比如jar包 通过 java -jar运行

自动禁用 开发工具集

[html] view plain copy
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>org.springframework.boot</groupId>  
  4.         <artifactId>spring-boot-devtools</artifactId>  
  5.         <optional>true</optional>  
  6.     </dependency>  
  7. </dependencies>  
在spring.properties添加启动配置 (添加了maven依赖 该参数默认为true  如果想禁用 改成false)

[html] view plain copy
  1. spring.devtools.restart.enabled=true  

springboot可以定制哪些资源会自动重启 哪些资源不会自动重启 (静态资源如图片等)

spring-boot-devtools-1.5.7.RELEASE.jar下DevToolsSettings中定义了开发工具的逻辑

[html] view plain copy
  1. 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 比如

[html] view plain copy
  1.   .   ____          _            __ _ _  
  2.  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \  
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  
  4.  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  
  5.   '  |____| .__|_| |_|_| |_\__, | / / / /  
  6.  =========|_|==============|___/=/_/_/_/  
  7.  :: 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为

[html] view plain copy
  1. ╭⌒╮¤ `   ${application.version}  
  2.   
  3. ╭╭ ⌒╮ ●╭○╮ ${spring-boot.version}  
  4.   
  5. ╰ ----╯/█∨█\   
  6.   
  7. ~~~~~~~~~~∏~~∏~~~~~~~~~~~  
运行后显示的效果为

[html] view plain copy
  1. 17:08:42.628 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []  
  2. 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/]  
  3. 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/]  
  4. ╭⌒╮¤ `   1290  
  5.   
  6. ╭╭ ⌒╮ ●╭○╮ 1.5.7.RELEASE  
  7.   
  8. ╰ ----╯/█∨█\   
  9.   
  10. ~~~~~~~~~~∏~~∏~~~~~~~~~~~  

三。随机值

springboot可以在properties文件中使用表达式产生一些随机值  比如

[html] view plain copy
  1. my.secret=${random.value}  
  2. my.number=${random.int}  
  3. my.bignumber=${random.long}  
  4. my.uuid=${random.uuid}  
  5. my.number.less.than.ten=${random.int(10)}  
  6. 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

添加接口和实现类

[html] view plain copy
  1. package cn.et.profile;  
  2. /**  
  3.  * 定义接口  
  4.  * @author jiaozi  
  5.  *  
  6.  */  
  7. public interface Db {  
  8.     String getName();  
  9. }  
  10. /**  
  11.  * 定义mysql实现类  
  12.  * @author jiaozi  
  13.  *  
  14.  */  
  15. class Mysql implements Db{  
  16.     @Override  
  17.     public String getName() {  
  18.         return "mysql";  
  19.     }  
  20.       
  21. }  
  22. /**  
  23.  * 定义oracle实现类  
  24.  * @author jiaozi  
  25.  *  
  26.  */  
  27. class Oracle implements Db{  
  28.     @Override  
  29.     public String getName() {  
  30.         return "oracle";  
  31.     }  
  32. }  
分别定义两个profile分别是开发环境(实例化mysql的bean)和生产环境(实例化oracle的bean)

开发环境profile定义

[html] view plain copy
  1. package cn.et.profile;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.context.annotation.Profile;  
  6.   
  7. @Profile(value="dev")  
  8. @Configuration  
  9. public class DataSourceBeanDev {  
  10.     @Bean  
  11.     public Db testBean() {  
  12.         return new Mysql();  
  13.     }  
  14. }  
生产环境profile定义

[html] view plain copy
  1. package cn.et.profile;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.context.annotation.Profile;  
  6.   
  7. @Profile(value="product")  
  8. @Configuration  
  9. public class DataSourceBeanProduct {  
  10.     @Bean  
  11.     public Db testBean() {  
  12.         return new Oracle();  
  13.     }  
  14. }  
添加一个控制层的rest类

[html] view plain copy
  1. package cn.et.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.web.bind.annotation.GetMapping;  
  5. import org.springframework.web.bind.annotation.PathVariable;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7.   
  8. import cn.et.profile.Db;  
  9.   
  10. @RestController  
  11. public class TestController {  
  12.   
  13.     @Autowired  
  14.     private Db db;   
  15.     @GetMapping("/q")  
  16.     public String query() {  
  17.         return db.toString()+"=="+db.getClass().getTypeName();  
  18.     }  
  19. }  

添加main方法

[html] view plain copy
  1. package cn.et;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.context.annotation.PropertySource;  
  6.   
  7. @SpringBootApplication  
  8. public class SbApplication {  
  9.   
  10.     public static void main(String[] args) {  
  11.         SpringApplication sa=new SpringApplication();  
  12.         sa.run(SbApplication.class, args);  
  13.           
  14.     }  
  15. }  
application.properties 添加当前启动激活的profile

[html] view plain copy
  1. 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

[html] view plain copy
  1. spring.profiles: dev  #激动dev  
  2. spring.profiles.include:#同时激活以下所有  
  3.   - devredis  
  4.   - devdb  
  5.   - devmongodb  

五。 日志配置

 springboot默认使用common-logging进行日志记录 日志分为以下几个级别

[html] view plain copy
  1. FATAL 错误可能导致程序崩溃  
  2. ERROR 一般为异常 程序继续运行  
  3. WARN 警告信息 非错误  
  4. INFO 程序正常运行记录信息  
  5. DEBUG 调试信息  
  6. TRACE 跟踪信息 级别最低  
  7. 一般设置为 某个级别 大于该级别之上所有级别日志都输出 级别从低到高依次为:  
  8. FATAL-DEBUG-INFO-WARN-ERROR -FATAL  
springboot默认的级别是INFO

通过在spring.properties中 设置debug=true设置级别为debug  trace=true设置级别为trace

其他的级别通过以下配置设置

[html] view plain copy
  1. logging.level.root=ERROR  

可以将日志定位输出到文件 (以下两个配置只能一个生效 都配置了 file生效)

[html] view plain copy
  1. logging.path=d:/logs #在该目录下生成 spring.log日志文件  
  2. logging.file=my.log #当前项目下 生成该文件  
其他的日志配置参考
[html] view plain copy
  1. https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#boot-features-logging  



原创粉丝点击