Spring Boot的特性

  1)、创建独立的Spring应用

  2)、直接嵌入Tomcat、Jetty或Undertow等Web容器(不需要部署WAR文件)

  3)、提供固化的starter依赖,简化构建配置和依赖管理

  4)、当条件满足时自动地装配Spring或第三方类库

  5)、提供运维(Production-Ready)特性,如指标信息(Metrics)、健康检查及外部化配置

  6)、绝无代码生成,并且不需要XML配置

即约定大于配置,简化开发。

  

为什么说是独立的Spring应用?

  SpringBoot应用无需再向传统的JavaEE应用那样,将应用打包成WAR文件或者JAR文件,并部署到JavaEE容器中运行(虽然其也支持)。

  SpringBoot应用采用嵌入式Web容器,独立于外部容器,对应用生命周期拥有完全自主的控制。

在传统的Spring应用中,外置容器需要启动脚本将其引导(如ContextLoaderListener),随其生命周期回调执行Spring上下文的初始化。比较代表性的是Spring Web中的

ContextLoaderListener和Web MVC中的DispatcherServlet,前者利用ServletContext生命周期构建Web ROOT Spring应用上下文,后者结合Servlet生命周期创建DispatcherServlet

的Spring应用上下文。无论何种方式,均属于被动的回调执行,这也是为什么它们没有完整的应用主导权的原因。

  当Spring Boot出现嵌入式容器启动方式后,嵌入式容器则称为应用的一部分,从本质上来说,它属于Spring应用上下文的组件Beans,这些组件和其他组件均由自动装配

特性Spring Bean定义(BeanDefinition),随Spring应用上下文启动而注册并初始化。而驱动Spring应用上下文启动的核心组件则是Spring Boot核心API SpringApplication,

所以是Spring应用,也可以称为SpringBoot应用。

理解自动装配

  SpringBoot引导类:


//@ImportResource 导入xml配置文件
@SpringBootApplication
public class SpboApplication { public static void main(String[] args) {
SpringApplication.run(SpboApplication.class, args);
}
}

@SpringBootApplication注解:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan  //SpringBoot会自动扫描当前类的同级包以及下级包里的Bean,并自动注入到Spring容器中
public @interface SpringBootApplication { /**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {}; /**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {}; /**
* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
* for a type-safe alternative to String-based package names.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {}; /**
* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {}; }

可以看到@SpringBootApplication注解是一个组合注解

相当于@Configuration、@EnableAutoConfiguration@ComponentScan的累加

  @EnableAutoConfiguration:激活Spring Boot自动装配机制

  @ComponentScan:激活@Component的扫描

  @Configuration:声明被标注为注解类

同时,其属性方法带有@AliasFor注解,用于桥接其他注解的属性。

@EnableAutoConfiguration注解可以帮我们自动载入应用程序所需要的所有默认配置

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration { /**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {}; /**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {}; }

此注解有两个重要的注解:

  @Import:向Spring容器中导入了EnableAutoConfigurationImportSelector组件

  @AutoConfigurationPackage:自动配置包

1):@AutoConfigurationPackage:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage { }

发现还是依靠Import注解导入了AutoConfigurationPackages.Registrar组件,其代码为:

    /**
* {@link ImportBeanDefinitionRegistrar} to store the base package from the importing
* configuration.
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
static class Registrar implements ImportBeanDefinitionRegistrar { @Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
register(registry, ClassUtils.getPackageName(metadata.getClassName()));
} }

其作用是将主配置类(@SpringBootApplication)的所在包及其子包里面的组件扫描到Spring容器中。比如带有@Entity注解的组件,由

@AutoConfigurationPackage扫描并加载,而平时常用的@Controller/@Service/@Component/@Repository这些注解是由ComponentScan

来扫描并加载的。

2):EnableAutoConfigurationImportSelector组件

  会在Spring启动的时候扫描所有jar路径下的META-INF/Spring.factories,然后筛选出以EnableAutoConfiguration为key的数据,加载到IOC

容器中,最后会默认加载113个默认的配置类,实现自动配置功能。

理解约定大于配置

  即通过约定来减少配置。约定优于配置是一个简单的概念。系统、类库、框架应该假定合理的默认值,而非要求提供不必要的配置。

大部分情况下,使用框架提供的默认值会让你的项目开发起来效率更快。如:

在Spring Boot中,当我们导入一个spring-boot-starter-web后。就会自动地帮我们导入Spring MVC的相关依赖(包括Json支持的Jackson和数据校验的HibernateValidator)

和一个内置的Tomcat容器,这使得开发阶段可以直接通过main方法或者JAR包独立运行一个WEB项目。因为Spring Boot约定,当你导入了一个spring-boot-starter-web后,

就约定了你是一个web开发环境。

获取属性配置文件的Properties

  在常规的Spring环境下,注入properties文件里的值要通过@PropertySource指明properties文件的位置,然后通过@Value注入值,在SpringBoot里,只需要在application.properties

 定义属性,直接使用@Value注入即可。但是如果我们的配置比较多的话,则@Value会注入很多次。所以SpringBoot还提供了基于类型安全的配置方式,通过@Configurationproperties

 将properties属性和一个Bean及其属性关联,从而实现类型安全的配置。

// 将前缀为self的属性配置和bean关联起来,使用时直接依赖注入此bean即可
@Component
@ConfigurationProperties(prefix = "self")
public class SelfProperties {
private String name;
private Long age;
get set...
}

SpringBoot编程思想的更多相关文章

  1. 读书笔记《SpringBoot编程思想》

    目录 一. springboot总览 1.springboot特性 2.准备运行环境 二.理解独立的spring应用 1.应用类型 2.@RestController 3.官网创建springboot ...

  2. JAVA编程思想(第四版)学习笔记----4.8 switch(知识点已更新)

    switch语句和if-else语句不同,switch语句可以有多个可能的执行路径.在第四版java编程思想介绍switch语句的语法格式时写到: switch (integral-selector) ...

  3. 《Java编程思想》学习笔记(二)——类加载及执行顺序

    <Java编程思想>学习笔记(二)--类加载及执行顺序 (这是很久之前写的,保存在印象笔记上,今天写在博客上.) 今天看Java编程思想,看到这样一道代码 //: OrderOfIniti ...

  4. #Java编程思想笔记(一)——static

    Java编程思想笔记(一)--static 看<Java编程思想>已经有一段时间了,一直以来都把笔记做在印象笔记上,今天开始写博客来记录. 第一篇笔记来写static关键字. static ...

  5. C语言之通过冒泡排序浅谈编程思想

    写这篇博文的目的是想起到抛砖引玉的作用,还请大牛们留下一些先进的思想,让小菜学习一下.下面入正题. 复习C语言怎么能少的了冒泡呢,记得刚学C语言那会,感觉冒泡排序真的太复杂了,理解不大了,嗯!还是当时 ...

  6. [Java编程思想-学习笔记]第3章 操作符

    3.1  更简单的打印语句 学习编程语言的通许遇到的第一个程序无非打印"Hello, world"了,然而在Java中要写成 System.out.println("He ...

  7. Java编程思想重点笔记(Java开发必看)

    Java编程思想重点笔记(Java开发必看)   Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而 ...

  8. 《java编程思想》读书笔记(一)开篇&第五章(1)

    2017 ---新篇章  今天终于找到阅读<java编程思想>这本书方法了,表示打开了一个新世界. 第一章:对象导论 内容不多但也有20页,主要是对整本书的一个概括.因为已经有过完整JAV ...

  9. 16条Web2.0法则的编程思想

    1.在你开始之前,先定一个简单的目标.无论你是一个Web 2.0应用的创建者还是用户,请清晰的构思你的目标.就像“我需要保存一个书签”或者“我准 备帮助人们创建可编辑的.共享的页面”这样的目标,让你保 ...

随机推荐

  1. 【运维】使用FileZilla搭建FTP服务器

    一.下载Filezilla  Server 官网网址:https://filezilla-project.org 二.安装Filezilla  Server   Filezilla  Server的安 ...

  2. Java中POI操作Excel常用方法

    1. https://blog.csdn.net/yjt520557/article/details/82763785 2. https://blog.csdn.net/zxh66/article/d ...

  3. noi.ac#228 book

    分析 代码 #include<bits/stdc++.h> using namespace std; #define int long long const int inf =1e18; ...

  4. 【ABAP系列】SAP ABAP模块-取整操作中CEIL和FLOOR用法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP模块-取整操作中 ...

  5. 【Unity系统知识】之unity文件操作路径

    IOS:Application.dataPath :                      Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx ...

  6. 什么是php扩展

    PHP扩展英文为PHP Extension and Application Repository,简称pear(下面都以pear简称),中文全称为PHP扩展与应用库.是为了创建一个类似于Perl CP ...

  7. mysql练习题目试水50题,附建库sql代码

    如果你没试过水的话,那一题一题地每一题都敲一遍吧.不管它们对你看来有多么简单.  建库代码 部分题目答案在末尾,可用ctrl f  搜索题号. 作业练习——学生-选课 表结构 学生表: Student ...

  8. HDFS-NameNode和SeconddaryNode

    一.NN和2N的工作机制 一.概述 一.概述 一.概述 一.概述 一.概述 一.概述 一.概述

  9. 2019 Multi-University Training Contest 1 - 1011 - Function - 数论

    http://acm.hdu.edu.cn/showproblem.php?pid=6588 新学到了一个求n以内与m的gcd的和的快速求法.也就是下面的S1. ①求: $ \sum\limits_{ ...

  10. 攻防世界--no-strings-attached

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/5d4117b968684b9483d0d4464e0a6fea 这道题要使用到gdb文 ...