1 Spring的优点分析

Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品。无需开发重量级的Enterprise JavaBean(EJB),Spring为企业级Java开发提供了一种相对简单的方法,通过依赖注入和面向切面编程,用简单的Java对象(Plain Old Java Object,POJO)实现了EJB的功能。

1.1SpringBoot的特点

  • 为基于Spring的开发提供更快的入门体验

  • 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求

  • 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等

  • SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式

1.2 SpringBoot的核心功能

  • 起步依赖

    起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。

    简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能。

  • 自动配置

    Spring Boot的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是Spring自动完成的。

SpringBoot快速入门

2.1 创建Maven工程

使用idea工具创建一个maven工程,该工程为普通的java工程即可

2.2 添加SpringBoot的起步依赖

SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent

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

SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

2.3 编写SpringBoot引导类

@SpringBootApplication
public class Springboot01Application { public static void main(String[] args) { SpringApplication.run(Springboot01Application.class, args);
}
}

3.1SpringBoot代码解析

  • @SpringBootApplication:标注SpringBoot的启动类,该注解具备多种功能(后面详细剖析)

  • SpringApplication.run(MySpringBootApplication.class) 代表运行SpringBoot的启动类,参数为SpringBoot启动类的字节码对象

3.2SpringBoot工程热部署

我们在开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大量的时间,我们可以在修改代码后不重启就能生效,在 pom.xml 中添加如下配置就可以实现这样的功能,我们称之为热部署。

<!--热部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

然后 Shift+Ctrl+Alt+/,选择Registry

4 起步依赖原理分析

4.1 分析spring-boot-starter-parent

<properties>
<activemq.version>5.15.3</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.63</appengine-sdk.version>
<artemis.version>2.4.0</artemis.version>
<aspectj.version>1.8.13</aspectj.version>
<assertj.version>3.9.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.7.11</byte-buddy.version>
... ... ...
</properties>

spring-boot-starter-dependencies的pom.xml中我们可以发现,一部分坐标的版本、依赖管理、插件管理已经定义好,所以我们的SpringBoot工程继承spring-boot-starter-parent后已经具备版本锁定等配置了。所以起步依赖的作用就是进行依赖的传递。

4.2 自动配置原理解析

@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}

注解@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 { /**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {}; ... ... ... }

其中,

@SpringBootConfiguration:等同与@Configuration,既标注该类是Spring的一个配置类

@EnableAutoConfiguration:SpringBoot自动配置功能开启

按住Ctrl点击查看注解@EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
... ... ...
}
@AutoConfigurationPackage自动加载了package org.springframework.boot.autoconfigure;包

application.properties配置文件加载过程

spring.factories 文件中有关自动配置的配置信息如下:

我们以ServletWebServerFactoryAutoConfiguration为例来分析源码:

找到ServletWebServerFactoryAutoConfiguration的实现

ServletWebServerFactoryAutoConfiguration 又自动使用了@EnableConfigurationProperties({ServerProperties.class})的配置属性

打开@EnableConfigurationProperties({ServerProperties.class})的配置属性类

其中,prefix = "server" 表示SpringBoot配置文件中的前缀,SpringBoot会将配置文件中以server开始的属性映射到该类的字段中。映射关系如下:

<你是我自罚三杯也不肯开口的秘密>

最新文章

  1. asp.net 预编译和动态编译
  2. CentOS SSH配置
  3. 用PYTHON输入输出字符串
  4. Hadoop学习笔记(1)概述
  5. Java模拟POST表单提交HttpClient操作
  6. Quick Cocos2dx Action相关
  7. arcpy.mapping常用四大件-StyleItem
  8. 《精通Spring 4.X企业应用开发实战》读书笔记1-1(IoC容器和Bean)
  9. fatal error C1083: Cannot open precompiled header file: &#39;Debug/xxoo.pch&#39;: No such file or directory
  10. 使用C++对物理网卡/虚拟网卡进行识别(包含内外网筛选)
  11. Road of computer tec 01
  12. npx
  13. python super超类方法
  14. 51nod1238 最小公倍数之和 V3
  15. golang channel 总结
  16. 每日scrum(7)
  17. python 多线程队列
  18. Windows下C++删除清除map
  19. Spring整合MyBatis(一)MyBatis独立使用
  20. 设计模式(17) 访问者模式(VISITOR) C++实现

热门文章

  1. javascript面向对象编程笔记(函数)
  2. Random类和Math.random()方法
  3. JS函数 有参数的函数 参数可以多个,根据需要增减参数个数。参数之间用(逗号,)隔开
  4. &lt;a&gt;标签中的href=&quot;&quot;
  5. Erlang学习记录:相关工具和文档
  6. scala中Tuple简单使用
  7. SpringCloudBus
  8. Makefile 从入门到放弃
  9. day25 模块,sys, logging, json, pickle
  10. Vue+Iview+Node 搭建数据模拟接口