SpringBoot总结

一.Spring 简史

  1. Spring 1.x 时代:在 Spring1.x 时代,都是通过 xml 文件配置 bean,随着项目的不断扩大,需要将 xml 配置分放到不同的配置文件中,需要频繁的在 java 类和 xml 配置文件中切换。
  2. Spring 2.x 时代:随着 JDK 1.5 带来的注解支持,Spring2.x 可以使用注解对 Bean 进行申明和注入,大大的减少了 xml 配置文件,同时也大大简化了项目的开发。

    那么,问题来了,究竟是应该使用 xml 还是注解呢?最佳实践:
    • 应用的基本配置用 xml,比如:数据源、资源文件等
    • 业务开发用注解,比如:Service 中注入 bean 等
  3. Spring 3.x 时代:从 Spring3.x 开始提供了 Java 配置方式,使用 Java 配置方式可以更好的理解你配置的 Bean,现在我们就处于这个时代,并且 Spring4.x 和 Spring boot 都推荐使用 java 配置的方式。

二.Spring Boot 简介

  1. 随着动态语言的流行 (Ruby、Groovy、Scala、Node.js),Java 的开发显得格外的笨重:繁多的配置、低下的开发效率、复杂的部署流程以及第三方技术集成难度大。
  2. 在上述环境下,Spring Boot 应运而生。它使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速的运行起来。使用 Spring Boot 很容易创建一个独立运行(运行 Jar,内嵌 Servlet 容器)准生产级别的基于 Spring 框架的项目,使用 Spring Boot 你可以不用或者只需很少的 Spring 配置。(减少了配置文件)

三.Spring Boot 的优点

  1. 快速构建项目
  2. 对主流开发框架的无配置集成
  3. 项目可独立运行,无需外部依赖 Servlet 容器
  4. 提供运行时的应用监控
  5. 极大地提高了开发、部署效率
  6. 与云计算的天然集成

四.Spring Boot 的缺点

  1. 坑多文档少
  2. 版本迭代速度很快,一些模块改动很大
  3. 由于不用自己做配置,报错时很难定位
  4. 网上现成的解决方案比较少

五.第一个 Spring Boot 应用程序

  1. 这里我们使用 Intellij IDEA 来新建一个 Spring Boot 项目。
  2. 新建 Spring Initializr 项

  3. 填写项目信息
  4. 选择项目使用技术,SpringBot版本可以到pom.xml文件中修改,后来我改成了1.5.8
  5. 创建好项目后进行测试。整个测试项目的目录结构
  6. 添加控制器
  7. 新建 Spring Boot 项目后,会在根包目录下有一个 artifactId + Application 命名规则的入口类,如上图。main() 方法:是一个标准的 Java 应用的 main 方法,主要作用是作为项目启动的入口。
  8. 运行效果:HelloApplication 文件中单击右键,在右键菜单中选择 Run "HelloApplication" 运行项目

     

六.Spring Boot 基本配置

  1.  自定义 Banner,

    我们在 src/main/resources 目录下新建一个 banner.txt

    通过 http://patorjk.com/software/taag 网站生成字符串,将网站生成的字符复制到 banner.txt 中。再次运行这个程序

                    _ooOoo_

                   o8888888o

                    88" . "88

                     (| -_- |)

                    O\  =  /O

               ____/`---'\____

                 .'  \\|     |//  `.

                /  \\|||  :  |||//  \

               /  _||||| -:- |||||-  \

               |   | \\\  -  /// |   |

               | \_|  ''\---/''  |   |

               \  .-\__  `-`  ___/-. /

             ___`. .'  /--.--\  `. . __

          ."" '<  `.___\_<|>_/___.'  >'"".

         | | :  `- \`.;`\ _ /`;.`/ - ` : | |

         \  \ `-.   \_ __\ /__ _/   .-` /  /

    ======`-.____`-.___\_____/___.-`____.-'======

                       `=---='

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

             佛祖保佑       永无BUG

     

     

    再次启动springboot项目后图案就会变成上面的图案

  2. Spring Boot 配置文件

    Spring Boot项目使用一个全局的配置文件 application.properties 或者是 application.yml,在resources 目录下或者类路径下的 /config 下,一般我们放到 resources 下。

    修改 tomcat 的端口为 9090,并将默认的访问路径 "/" 修改为 "boot",可以在 application.properties 中添加:

    server.port=9090
    server.context-path=/boot

    或在 application.yml 中添加:

    server:
      port:
    9090
      context-path: /boot      #虚拟目录也就是我们学过的自定义的命名空间只不过这里是全局的

    测试效果

     

    修改 DispatcherServlet 的规则为:*.html

    server:
    
      port: 9090
    
      context-path: /boot    #虚拟目录也就是我们学过的自定义的命名空间只不过这里是全局的
    
      servlet-path: "*.html"   #后缀必须以.html结尾

    测试效果

     

    更多配置:https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/html/common-application-properties.html

  3. starter pom(就是pom.xml文件)

    Spring Boot 为我们提供了简化企业级开发绝大多数场景的 starter pom ,只要使用了应用场景所需要的 starter pom ,相关的技术配置将会消除,就可以得到 Spring Boot 为我们提供的自动配置的 Bean。

  4. 官方提供的 starter pom

    https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/html/using-boot-build-systems.html#using-boot-starter

    Name

    Description

    Pom

    spring-boot-starter

    Core starter, including auto-configuration support, logging and YAML

    Pom

    spring-boot-starter-activemq

    Starter for JMS messaging using Apache ActiveMQ

    Pom

    spring-boot-starter-amqp

    Starter for using Spring AMQP and Rabbit MQ

    Pom

    spring-boot-starter-aop

    Starter for aspect-oriented programming with Spring AOP and AspectJ

    Pom

    spring-boot-starter-artemis

    Starter for JMS messaging using Apache Artemis

    Pom

    spring-boot-starter-batch

    Starter for using Spring Batch

    Pom

    spring-boot-starter-cache

    Starter for using Spring Framework’s caching support

    Pom

    spring-boot-starter-cloud-connectors

    Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku

    Pom

    spring-boot-starter-data-cassandra

    Starter for using Cassandra distributed database and Spring Data Cassandra

    Pom

    spring-boot-starter-data-couchbase

    Starter for using Couchbase document-oriented database and Spring Data Couchbase

    Pom

    spring-boot-starter-data-elasticsearch

    Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch

    Pom

    spring-boot-starter-data-gemfire

    Starter for using GemFire distributed data store and Spring Data GemFire

    Pom

    spring-boot-starter-data-jpa

    Starter for using Spring Data JPA with Hibernate

    Pom

    spring-boot-starter-data-ldap

    Starter for using Spring Data LDAP

    Pom

    spring-boot-starter-data-mongodb

    Starter for using MongoDB document-oriented database and Spring Data MongoDB

    Pom

    spring-boot-starter-data-neo4j

    Starter for using Neo4j graph database and Spring Data Neo4j

    Pom

    spring-boot-starter-data-redis

    Starter for using Redis key-value data store with Spring Data Redis and the Jedis client

    Pom

    spring-boot-starter-data-rest

    Starter for exposing Spring Data repositories over REST using Spring Data REST

    Pom

    spring-boot-starter-data-solr

    Starter for using the Apache Solr search platform with Spring Data Solr

    Pom

    spring-boot-starter-freemarker

    Starter for building MVC web applications using FreeMarker views

    Pom

    spring-boot-starter-groovy-templates

    Starter for building MVC web applications using Groovy Templates views

    Pom

    spring-boot-starter-hateoas

    Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS

    Pom

    spring-boot-starter-integration

    Starter for using Spring Integration

    Pom

    spring-boot-starter-jdbc

    Starter for using JDBC with the Tomcat JDBC connection pool

    Pom

    spring-boot-starter-jersey

    Starter for building RESTful web applications using JAX-RS and Jersey. An alternative tospring-boot-starter-web

    Pom

    spring-boot-starter-jooq

    Starter for using jOOQ to access SQL databases. An alternative tospring-boot-starter-data-jpaorspring-boot-starter-jdbc

    Pom

    spring-boot-starter-jta-atomikos

    Starter for JTA transactions using Atomikos

    Pom

    spring-boot-starter-jta-bitronix

    Starter for JTA transactions using Bitronix

    Pom

    spring-boot-starter-jta-narayana

    Spring Boot Narayana JTA Starter

    Pom

    spring-boot-starter-mail

    Starter for using Java Mail and Spring Framework’s email sending support

    Pom

    spring-boot-starter-mobile

    Starter for building web applications using Spring Mobile

    Pom

    spring-boot-starter-mustache

    Starter for building MVC web applications using Mustache views

    Pom

    spring-boot-starter-security

    Starter for using Spring Security

    Pom

    spring-boot-starter-social-facebook

    Starter for using Spring Social Facebook

    Pom

    spring-boot-starter-social-linkedin

    Stater for using Spring Social LinkedIn

    Pom

    spring-boot-starter-social-twitter

    Starter for using Spring Social Twitter

    Pom

    spring-boot-starter-test

    Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito

    Pom

    spring-boot-starter-thymeleaf

    Starter for building MVC web applications using Thymeleaf views

    Pom

    spring-boot-starter-validation

    Starter for using Java Bean Validation with Hibernate Validator

    Pom

    spring-boot-starter-web

    Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container

    Pom

    spring-boot-starter-web-services

    Starter for using Spring Web Services

    Pom

    spring-boot-starter-websocket

    Starter for building WebSocket applications using Spring Framework’s WebSocket support

    Pom

     

     

     

    Name

    Description

    Pom

    spring-boot-starter-actuator

    Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application

    Pom

    spring-boot-starter-remote-shell

    Starter for using the CRaSH remote shell to monitor and manage your application over SSH. Deprecated since 1.5

    Pom

     

     

     

    Name

    Description

    Pom

    spring-boot-starter-jetty

    Starter for using Jetty as the embedded servlet container. An alternative tospring-boot-starter-tomcat

    Pom

    spring-boot-starter-log4j2

    Starter for using Log4j2 for logging. An alternative tospring-boot-starter-logging

    Pom

    spring-boot-starter-logging

    Starter for logging using Logback. Default logging starter

    Pom

    spring-boot-starter-tomcat

    Starter for using Tomcat as the embedded servlet container. Default servlet container starter used byspring-boot-starter-web

    Pom

    spring-boot-starter-undertow

    Starter for using Undertow as the embedded servlet container. An alternative tospring-boot-starter-tomcat

    Pom

  5. Spring Boot 日志配置

    Spring Boot 对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置

    默认情况下,Spring Boot 使用 Logback 作为日志框架

  6. 配置日志文件

    logging:
      file: ../logs/spring-boot-hello.log
      level.org.springframework.web: DEBUG
  7. 关闭特定的自动配置

    关闭特定的自动配置使用 @SpringBootApplication 注解的 exclude 参数

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

    说明:这里是关闭数据源的自动配置

 

posted @ 2019-08-08 11:50  Java-Legend  阅读(424)  评论(0编辑  收藏  举报