前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot一些很实用的功能

SpringBoot一些很实用的功能

作者头像
lyb-geek
发布2018-09-27 09:38:38
1.6K0
发布2018-09-27 09:38:38
举报
文章被收录于专栏:Linyb极客之路Linyb极客之路

一、定制Banner

springboot在启动的时候,会有下面这个图片

然后这个图案是可以自定义的,在src/main/resource下新建一个banner.txt文件,然后去http://patorjk.com/software/taag/下自定义自己所要显示的文字,然后点击下面的select & copy把复制的内容复制到banner.txt文件中,重启一下Springboot,图标就会发生变化。

当然这个图案也是可以关闭的,在之前的版本,可以通过下面的方式关闭,但是在1.5.6,就是我现在学习的版本,这种方法是不可以的。

代码语言:javascript
复制
public static void main(String[] args) {  
    //        SpringApplication.run(Application.class, args);  
            SpringApplication application = new SpringApplication(Application.class);  
            application.setShowBanner(false);  
            application.run(args);  
        }

在SpringBoot1.5.6中,可以使用下面的方式关闭该图案

代码语言:javascript
复制
public static void main(String[] args) {
        new SpringApplicationBuilder(Demo2Application.class)
            .bannerMode(Mode.OFF)
            .run(args);
    }

或者

代码语言:javascript
复制
public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Demo2Application.class);
        application.setBannerMode(Mode.OFF);
        application.run(args);
    }

同时可以在banner.txt这个文件中添加下面这些东西显示当前的使用的SpringBoot的版本号

代码语言:javascript
复制
${spring-boot.version}           #1.5.6.RELEASE    
    ${spring-boot.formatted-version} # (v1.5.6.RELEASE)

二、application.yml

在新的文件下,会有一个application.properties,这大概是Springboot中唯一的一个配置文件了吧。这个文件默认的是空的。 在application.properties文件中添加下面的信息

代码语言:javascript
复制
server.port=8888
   server.context-path=/demo_2

这里主要是修改server的端口号和路径。

还有一种是application.yml文件,把原来的文件删除,或者移动到其他地方,然后在根目录(同级目录src/main/resources)下添加application.yml,在yml中按照下面这么配置。

代码语言:javascript
复制
server:
  port: 8443
  context-path: /demo_2

这个配置和properties实现的是一个功能,但是从文件上看,yml文件似乎更简洁,而且不管是eclipse还是idea中都支持对yml文件提示和自动补全,所以现在使用yml文件居多,所以接下来我都是使用yml文件配置的。

三、配置logback

在springBoot支持大部分日志配置,包括slf,log4j等日志框架,默认是使用logback,其他的日志框架百度一下都是有的,这里不做记录,主要就讲一下logback的配置

代码语言:javascript
复制
logging:
  file: logfile/log.log
  level: org.springframework.web = DEBUG

配置一下上面的信息一下,重启一下服务,刷新一下项目,就可以看到logfile/log.log文件,里面是日志信息。

四、热部署

springboot本身是不支持热部署的,每一次修改程序之后都需要重启一下项目,这样开发效率就会很低,所以热部署还是很有必要的。

在pom.xml文件,添加下面的配置:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <!--
    <optional>true</optional>
    optional=true,依赖不会传递,该项目依赖devtools;
    之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 
    -->
</dependency>

然后springboot就支持热部署了。每当发现class文件被修改,项目就会被重新加载。

五、profile配置

有时候开发和上线用的配置文件application.yml时不一样的,但是修改yml有时候会很麻烦。可以用下面的方式进行配置

在src/main/resources文件下添加下面两个文件,注意application-是不能改的,后面的可以自定义

代码语言:javascript
复制
application-dev.yml
application-pro.yml

然后在application.yml文件中只写入下面的东西就可以了。

代码语言:javascript
复制
spring:
  profiles:
    active:
      - pro

这样就可以将application-pro.yml加载进项目。 在我这短期这个功能是用不上的感觉。

六、替换favicon

springboot中默认的logo是一个绿叶形状的

,但是在实际中这个logo常常是需要替换成自己公司或者项目的图标的。

代码语言:javascript
复制
#下面是关闭图标,但是失败了,还是会有
spring:
  mvc:
    favicon:
      enabled: false

官方文档中说用这个方法可以去掉,但是1.5.6中,这种方法这种方向是不可以的。所以目前来说我还不知道怎么去掉这个logo,但是可以替换。

将自己的图案重命名为favicon.ico,然后放在static文件夹中。 在springboot中static是用来放资源的,templates是用来放页面的。 然后在每个页面中添加

代码语言:javascript
复制
<link rel="icon" type="image/x-icon" href="favicon.ico" />

这样就可以把logo替换掉,但是毕竟这种方式还是不是很方便,但是目前我就知道这种。

七、starter pom

名称

描述

spring-boot-starter

核心Spring Boot starter,包括自动配置支持,日志和YAML

spring-boot-starter-actuator

生产准备的特性,用于帮你监控和管理应用

spring-boot-starter-amqp

对”高级消息队列协议”的支持,通过spring-rabbit实现

spring-boot-starter-aop

对面向切面编程的支持,包括spring-aop和AspectJ

spring-boot-starter-batch

对Spring Batch的支持,包括HSQLDB数据库

spring-boot-starter-cloud-connectors

对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接

spring-boot-starter-data-elasticsearch

对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch

spring-boot-starter-data-gemfire

对GemFire分布式数据存储的支持,包括spring-data-gemfire

spring-boot-starter-data-jpa

对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate

spring-boot-starter-data-mongodb

对MongoDB NOSQL数据库的支持,包括spring-data-mongodb

spring-boot-starter-data-rest

对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现

spring-boot-starter-data-solr

对Apache Solr搜索平台的支持,包括spring-data-solr

spring-boot-starter-freemarker

对FreeMarker模板引擎的支持

spring-boot-starter-groovy-templates

对Groovy模板引擎的支持

spring-boot-starter-hateoas

对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现

spring-boot-starter-hornetq

对”Java消息服务API”的支持,通过HornetQ实现

spring-boot-starter-integration

对普通spring-integration模块的支持

spring-boot-starter-jdbc

对JDBC数据库的支持

spring-boot-starter-jersey

对Jersey RESTful Web服务框架的支持

spring-boot-starter-jta-atomikos

对JTA分布式事务的支持,通过Atomikos实现

spring-boot-starter-jta-bitronix

对JTA分布式事务的支持,通过Bitronix实现

spring-boot-starter-mail

对javax.mail的支持

spring-boot-starter-mobile

对spring-mobile的支持

spring-boot-starter-mustache

对Mustache模板引擎的支持

spring-boot-starter-redis

对REDIS键值数据存储的支持,包括spring-redis

spring-boot-starter-security

对spring-security的支持

spring-boot-starter-social-facebook

对spring-social-facebook的支持

spring-boot-starter-social-linkedin

对spring-social-linkedin的支持

spring-boot-starter-social-twitter

对spring-social-twitter的支持

spring-boot-starter-test

对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块

spring-boot-starter-thymeleaf

对Thymeleaf模板引擎的支持,包括和Spring的集成

spring-boot-starter-velocity

对Velocity模板引擎的支持

spring-boot-starter-web

对全栈web开发的支持,包括Tomcat和spring-webmvc

spring-boot-starter-websocket

对WebSocket开发的支持

spring-boot-starter-ws

对Spring Web服务的支持

spring-boot-starter-actuator

添加生产准备特性,比如指标和监控

spring-boot-starter-remote-shell

添加远程ssh shell支持

spring-boot-starter-jetty

导入Jetty HTTP引擎(作为Tomcat的替代)

spring-boot-starter-log4j

对Log4J日志系统的支持

spring-boot-starter-logging

导入Spring Boot的默认日志系统(Logback)

spring-boot-starter-tomcat

导入Spring Boot的默认HTTP引擎(Tomcat)

spring-boot-starter-undertow

导入Undertow HTTP引擎(作为Tomcat的替代)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、定制Banner
  • 二、application.yml
  • 三、配置logback
  • 四、热部署
  • 五、profile配置
  • 六、替换favicon
  • 七、starter pom
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档