SpringBoot学习(二)SpringBoot一些很实用的功能

一、定制Banner

springboot在启动的时候,会有下面这个图片
这里写图片描述
然后这个图案是可以自定义的,在src/main/resource下新建一个banner.txt文件,然后去http://patorjk.com/software/taag/下自定义自己所要显示的文字,然后点击下面的select & copy把复制的内容复制到banner.txt文件中,重启一下Springboot,图标就会发生变化。
这里写图片描述
当然这个图案也是可以关闭的,在之前的版本,可以通过下面的方式关闭,但是在1.5.6,就是我现在学习的版本,这种方法是不可以的。


    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中,可以使用下面的方式关闭该图案

    public static void main(String[] args) {
        new SpringApplicationBuilder(Demo2Application.class)
            .bannerMode(Mode.OFF)
            .run(args);
    }

或者

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Demo2Application.class);
        application.setBannerMode(Mode.OFF);
        application.run(args);
    }

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

    ${spring-boot.version}           #1.5.6.RELEASE    
    ${spring-boot.formatted-version} # (v1.5.6.RELEASE)

二、application.yml

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

server.port=8888
server.context-path=/demo_2

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

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

server:
  port: 8443
  context-path: /demo_2

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

三、配置logback

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

logging:
  file: logfile/log.log
  level: org.springframework.web = DEBUG

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

四、热部署

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

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

<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-是不能改的,后面的可以自定义

application-dev.yml
application-pro.yml

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

spring:
  profiles:
    active:
      - pro

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

六、替换favicon

springboot中默认的logo是一个绿叶形状的这里写图片描述,但是在实际中这个logo常常是需要替换成自己公司或者项目的图标的。

#下面是关闭图标,但是失败了,还是会有
spring:
  mvc:
    favicon:
      enabled: false

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

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

<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的替代)

这是我的源码,有兴趣的可以下载看看http://download.csdn.net/download/q15150676766/9924308

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值