简介

Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭 建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 配置。通过这种方式,Spring Boot 致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。其特点如下:

  1. 创建独立的Spring应用程序
  2. 嵌入的Tomcat,无需部署war文件
  3. 简化Maven配置
  4. 自动配置Spring
  5. 提供生产就绪型功能,如指标、健康检查和外部配置
  6. 没有代码生成和不要求配置xml

环境搭建

  1. 创建maven工程(不用骨架,打包方式jar)

  2. 添加起步依赖

    继承SpringBoot的起步依赖

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    </parent>

    导入web的启动依赖

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>
  3. 编写引导类(任意命名)

    @SpringBootApplication
    public class NewSpringBootApplication{
    public static void main(String[] args){
    SpringApplication.run(NewMySpringBootApplication.class);
    }
    }
  4. 编写Controller

    @Controller
    public class HelloController{
    @RequestMapping("/hello")
    @ResponseBody //说明返回的是json字符串
    public String hello(){
    return "hello springboot";
    }
    }

注意事项

  1. 所有的SpringBoot项目必须继承SpringBoot的起步依赖

  2. 添加依赖时以功能为单位

  3. @SpringBootApplication 声明该类是一个SpringBoot引导类

  4. SpringApplication.run(引导类.class) 表示运行SpringBoot的引导类(引导类和main方法可以分开)

  5. SpringBoot工程的热部署,更改代码后不需要重启项目即可生效

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    导入坐标后设置IDEA自动编译:

    • Settings --> Compiler --> select Build project automatically --> apply

    • press control+shift+/ --> Registry --> select compiler.automake.allow.when.app.running

  6. IDEA快速创建SpringBoot项目

    new project --> Spring Initializr --> select dependencies --> [delete .gitignore & mvnw & mvnw.cmd] --> create Controller

  7. @RestController = @Controller + @ResponseBody

原理分析

起步依赖

  • parent:当前项目 --> 继承spring-boot-starter-parent(处理配置文件,插件管理) --> 继承spring-boot-dependencies(maven 版本约定,依赖管理)

  • web:spring-boot-starter-web-2.0.1.RELEASE.pom中导入了tomcat,json等坐标

自动配置

  • @SpringBootApplication

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration //配置类
    @EnableAutoConfiguration //允许自动配置
    @ComponentScan( //自动扫描组件,约定引导类所在的包及子包所有的类都会扫描
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
    ), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    public @interface SpringBootApplication {...}
  • @SpringBootConfiguration

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration //spring的配置类注解
    public @interface SpringBootConfiguration {...}
  • @EnableAutoConfiguration

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import({AutoConfigurationImportSelector.class}) //当前配置类引入其他配置类
    public @interface EnableAutoConfiguration {...}
  • AutoConfiturationImportSelector.class

        public String[] selectImports(AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
    return NO_IMPORTS;
    } else {
    AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
    //调用getAutoConfigurationEntry()
    AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);
    return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }
    } protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
    return EMPTY_ENTRY;
    } else {
    AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
    //调用getCandidateConfigurations(),返回配置类的全包名
    List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
    configurations = this.removeDuplicates(configurations);
    Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
    this.checkExcludedClasses(configurations, exclusions);
    configurations.removeAll(exclusions);
    configurations = this.filter(configurations, autoConfigurationMetadata);
    this.fireAutoConfigurationImportEvents(configurations, exclusions);
    return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
    }
    } protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
    //错误信息说明了配置类的信息存在spring.factories中
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
    return configurations;
    }
  • org.springframework.boot:spring-boot-autoconfigure:2.2.2.RELEASE/spring-boot-autoconfigure-2.2.2.RELEASE.jar/META-INF/spring.factories

    ...
    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
    org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
    org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
    ...
  • 流程

    1. 通过配置文件提前进行默认配置
    2. 用ServerProperties类加载配置
    3. 用XxxAutoConfiguration类引入
    4. 用spring.factories存入多个AutoConfiguration全限定类名
    5. AutoConfigurationImportSelector类加载spring.factories获取配置信息
    6. @EnableAutoConfiguration引入AutoConfigurationImportSelector完成自动配置
  • 覆盖配置

    spring-boot-starter-parent-2.0.1.RELEASE.pom中include标签有application*.yml(.yaml/.properties),可以通过自定义对应配置文件进行覆盖

配置文件

  • 位置:resources目录下

  • 顺序:多个配置文件时,yml > yaml > properties,后加载的会覆盖之前的

  • yml语法

    # 普通数据
    name: whteway
    # 对象(常用)
    user:
    username: root
    password: 1234
    # 行内对象配置
    user: {username: root,password: 1234}
  • 读取配置信息

    @Value 精确,繁琐

    @Value("${name}")
    private String name;
    @Value("${user.username}")
    private String username;

    @ConfigurationProperties 自动封装,需要配置configuration processor

    @Controller
    @ConfigurationProperties(prefix="user")
    public class LoginController{
    private String username;
    private String password;
    //generate getters and setters
    }
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
    </dependency>

集成Mybatis

  1. 添加起步依赖

    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
    </dependency>
  2. 添加数据库驱动坐标

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
  3. 添加数据库连接信息,application.yml

    spring:
    datasource:
    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/dbName?useUnicode=true?characterEncoding=utf8
    username=root
    password=root
  4. 建库建表

  5. 配置映射关系

    UserMapper.xml

  6. SpringBoot的配置文件中配置Mybatis的信息

    mybatis:
    type-aliases-package=com.whteway.entity
    mapper-locations=classpath:mapper/*Mapper.xml

集成Junit

  1. 添加起步依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
  2. 编写测试类

    @RunWith(SpringRunner.class)
    @SpringBootTes(classes="引导类全包名")
    public class Test{
    @Test
    public void test(){...}
    }

集成SpringDataJPA

  1. 添加起步依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
  2. 添加数据库驱动坐标

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
  3. 添加数据库连接信息,application.yml

    spring:
    datasource:
    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/dbName
    username=root
    password=root
  4. 建库建表

  5. 建实体类

    @Entity
    public class User{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String username;
    ...
    }
  6. 建接口

    public interface UserRepository extends JpaRepository<User, Long>{
    public List<User> findAll();
    }
  7. SpringBoot的配置文件中配置Mybatis的信息

    spring:
    jpa:
    database: MySQL
    show-sql: true
    generate-ddl: true
    hibernate:
    ddl-auto: update
    naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy

集成Redis

  1. 添加起步依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
  2. 配置连接信息

    spring:
    redis:
    host=127.0.0.1
    port=6379
  3. 测试

    @RunWith(SpringRunner.class)
    @SpringBootTes(classes="引导类全包名")
    public class Test{ @Autowired
    private RedisTemplate<String, String> redisTemplate; @Autowired
    private UserRepository userRepository; @Test
    public void test(){
    String userListJson = redisTemplate.boundValueOps("user.findAll").get();
    if(null==userListJson){
    List<User> all = userRepository.findAll();
    ObjectMapper objectMapper = new ObjectMapper();
    userListJson = objectMapper.writeValueAsString(all);
    redisTemplate.boundValueOps("user.findAll").set(userListJson);
    }
    System.out.println(userListJson);
    }
    }

Spring Boot 复习的更多相关文章

  1. [Java复习] Spring Boot

    什么是Spring Boot? 传统SSH/SSM框架配置繁琐,有很多重复的模板配置,效率不高. Spring Boot快速创建可独立运行,生产级别的Spring应用程序. 主要是基于Spring家族 ...

  2. Spring Boot 2.x整合Redis

    最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...

  3. Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统

    一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...

  4. 如何做自己的服务监控?spring boot 2.x服务监控揭秘

    Actuator是spring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...

  5. spring boot 系列之一:spring boot 入门

    最近在学习spring boot,感觉确实很好用,开发环境搭建和部署确实省去了很多不必须要的重复劳动. 接下来就让我们一起来复习下. 一.什么是spring boot ? spring boot是干嘛 ...

  6. spring boot 搭建基本套路《1》

    1. Spring复习 Spring主要是创建对象和管理对象的框架. Spring通过DI实现了IoC. Spring能很大程度的实现解耦. 需要掌握SET方式注入属性的值. 需要理解自动装配. 需要 ...

  7. 面试那点小事,你从未见过的spring boot面试集锦(附详细答案)

    一, 什么是spring boot? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问页面https://spring.io/projects,我们将看到所有在应用程序中使用的不同功能的 ...

  8. Spring Boot 2.x基础教程:JSR-303实现请求参数校验

    请求参数的校验是很多新手开发非常容易犯错,或存在较多改进点的常见场景.比较常见的问题主要表现在以下几个方面: 仅依靠前端框架解决参数校验,缺失服务端的校验.这种情况常见于需要同时开发前后端的时候,虽然 ...

  9. 2019年Spring Boot面试都问了什么?快看看这22道面试题!

    Spring Boot 面试题 1.什么是 Spring Boot? 2.Spring Boot 有哪些优点? 3.什么是 JavaConfig? 4.如何重新加载 Spring Boot 上的更改, ...

随机推荐

  1. 使用Psi Probe监控Tomcat8.5

    一.从GitHub上下载Psi Probe的war包 https://github.com/psi-probe/psi-probe/releases 可以看到当前最新版是3.3.1,下载 probe. ...

  2. mysql之子查询、视图、事务及pymysql等

    数据准备 CREATE TABLE `emp` ( `id` int(0) NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL, `gender` ...

  3. python基础之八:知识小结及补充

    一.python2 与python3 的区别 1.在2中print后可带扣号,也可不带,3中必须带,否则报错! #print 'hello python2' print('hello python3' ...

  4. Windbg Register(寄存器)窗口的使用

    寄存器是位于在 CPU 的小易失性内存单位. 许多寄存器专用于特定用途,并可用于用户模式应用程序使用的其他寄存器. 基于 x86 和基于 x64 的处理器在有可用的寄存器的不同集合. 如何打开寄存器窗 ...

  5. ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于(GPRS模块AT指令TCP透传方式),定时访问升级(兼容Air202,SIM800)

    实现功能概要 单片机定时使用http访问云端的程序版本, 如果版本不一致,然后通过http下载最新的升级文件,实现远程升级STM32. 兼容Air202,SIM800 测试准备工作(默认访问我的服务器 ...

  6. 8-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案安全篇(Apache 配置SSL,HTTPS连接)

    https://www.cnblogs.com/yangfengwu/p/10947423.html 和当时配置MQTT差不多,去下载证书文件   https://www.cnblogs.com/ya ...

  7. IE和火狐的事件机制有什么区别

    1.IE的事件流是冒泡流,火狐支持冒泡流和捕获流. 2.阻止事件冒泡:IE---e.cancelBubble = true;    火狐---e.stopPropagation();

  8. UNIX网络编程卷1 - >环境搭建(ubuntu16.04)

      学习unp网络编程,树上的例子均存在#include“unp.h”,故需要对环境进行配置. 1.到资源页下载www.unpbook.com 2.解压并将unpv13e移动到相应的文件夹下 (因为我 ...

  9. shell 命令行

    转:Davygeek 1. -eq           //等于 -ne           //不等于 -gt            //大于 (greater ) -lt            / ...

  10. 使Jackson和Mybatis支持JSR310标准

    1.首先要确保Jackson和Mybatis正确地整合进项目了 2.添加额外的依赖 <dependency> <groupId>org.mybatis</groupId& ...