zoukankan      html  css  js  c++  java
  • Spring Boot 概念知识

    转 http://rapharino.com/coder/Spring-Boot-Induction/

    Spring Boot Induction

    发表于   |   分类于   |     |   阅读次数 45 

    Spring Boot简化了基于Spring的应用开发,尝试封装 Spring 可怕繁杂的配置,以尽量少的配置创建一个独立的,产品级别的Spring应用。 Spring Boot使得Spring应用变的更轻量化,自动化,并为Spring平台及第三方库提供开箱即用的配置.Spring Boot 并未引入任何形式的代码生成技术,而是利用了 Spring4 的特性和项目构建实践 的优秀集成应用框架.因此本质上还是 Spring.

    目标

    • 为所有Spring开发提供一个从根本上更快,且随处可得的入门体验。
    • 开箱即用,但通过不采用默认设置可以快速摆脱这种方式。
    • 提供一系列大型项目常用的非功能性特征,比如:内嵌服务器,安全,指标,健康检测,外部化配置。
    • 没有冗余代码生成,也不需要XML配置。

    特性

    • 自动配置: 针对常用的功能,提供默认配置.以满足最基础功能.
    • 起步依赖: 提供大量的扩展库, 实现各个功能.
    • 命令行界面: 可选特性,无需构建项目, 实现 SpringBoot的动态编译和运行.
    • Actuator : 提供基础监控服务,提供运行时的检视应用内部情况的能力.包括
      • 上下文中的 Bean
      • 自动配置策略(条件化配置)
      • 环境变量,系统属性,配置属性,命令行参数 等.
      • 线程状态
      • HTTP 调用情况记录
      • 内存用量,垃圾回收,请求等相关指标.

    快速入门

    Spring Boot 极力的贴合 约定优于配置的设计范式.下面是一个 Demo,以最少的代码,实现一个 Web Service.

    maven 构建项目

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <!-- boot parent -->
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
    <!-- boot starter -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- web starter -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    编写代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @RestController
    @EnableAutoConfiguration
    public class App {
    @RequestMapping("/")
    String home() {
    return "Hello World!";
    }
    public static void main(String[] args) throws Exception {
    SpringApplication.run(App.class, args);
    }
    }

    如果使用浏览器打开localhost:8080,你应该可以看到如下输出:

    1
    Hello World!

    启动流程

    rp-core 中的 SpringApplication 的实现方式和 SpringBoot 类似,主要借鉴其 自动配置,ApplicationContext生命周期管理功能.

    1. xml 或者 java code 实现对象构建和依赖绑定的描述信息.

    2. 创建AnnotationConfigApplicationContext(或者AnnotationConfigEmbeddedWebApplicationContext) (上图错误.)

    3. SpringFactoriesLoader.loadFactoryNames()读取了ClassPath下面的META-INF/spring.factories文件.其中主要包括ApplicationContextInitializer,ApplicationListener,和@Configuration 其相关实现类(注解类).

    4. 为 ApplicationContext 注册ApplicationContextInitializer.ApplicationListener.(原生 Spring 提供的扩展)

    5. 启动 自动配置流程

      1. SpringBootApplication注解中带有EnableAutoConfiguration注解

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        @Target(ElementType.TYPE)
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        @Inherited
        @AutoConfigurationPackage
        @Import(EnableAutoConfigurationImportSelector.class)
        public @interface EnableAutoConfiguration {
        String ENABLED_OVERRIDE_PROPERTY = "spring.boot.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 {};
        }
      2. EnableAutoConfiguration 为容器注册了一个EnableAutoConfigurationImportSelector对象.它则是自动配置的核心类.

      3. EnableAutoConfigurationImportSelector实现了ImportSelector的selectImports方法:

        1. SpringFactoriesLoader.loadFactoryNames()获取配置相关的元数据.

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          # 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,
          org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,
          org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,
          org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,
          org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,
          ......
          org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

        2. 必要的处理.如去重,排序,条件依赖.

        3. 返回交由容器注册相关实例对象.

      4. 容器初始化完成.(额外会提供点周边服务,如 CommandLineRunner)

    外置配置

    SpringBoot 兼顾 Spring的强大功能,并且提供极强的扩展性.在 SpringBoot及其周边项目有大量的 Starter模块,实现各个功能.如 spring-boot-starter-web,spring-boot-starter-jmx,spring-boot-starter-mybatis.他们有自己的默认配置,也可以通过 Spring 的属性 API 进行自定义.

    属性API

    • PropertySource:属性源,key-value属性对抽象,比如用于配置数据
    • PropertyResolver:属性解析器,用于解析相应key的value
    • Environment:环境,本身是一个PropertyResolver,但是提供了Profile特性,即可以根据环境得到相应数据(即激活不同的Profile,可以得到不同的属性数据,比如用于多环境场景的配置(正式机、测试机、开发机DataSource配置))
    • Profile:剖面,只有激活的剖面的组件/配置才会注册到Spring容器,类似于maven中profile

    配置读取顺序

    SpringBoot 配置使用一个特别的顺序进行合理的配置和覆盖.允许开发者通过配置服务,静态文件,环境变量,命令行参数,注解类等,用以外化配置.覆盖顺序如下:

    1. 命令行参数
    2. 来自于 java:comp/env 的 JNDI 属性
    3. 操作系统环境变量
    4. 在打包的 jar 外的配置文件(包括 propertis,yaml,profile变量.)
    5. 在打包的 jar 内的配置文件(包括 propertis,yaml,profile变量.)
    6. @Configuration 类的@PropertySource 注解
    7. 应用框架默认配置

    application.properties和application.yml文件能放在以下四个位置。亦拥有覆盖顺序(高 - 低)

    • 外置,在相对于应用程序运行目录的/config子目录里。
    • 外置,在应用程序运行的目录里。
    • 内置,在config包内。
    • 内置,在Classpath根目录。

    每个模块都有自己的配置.在此不再赘述.

    Actuator

    Actuator端点,实现应用内部信息监控检测.实际开发和生产(必要控制)场景下,它的作用必不可少.

    AD3362AA-6045-4C59-A763-CAF849464AF1

    9A86C781-0629-4D8D-90B4-B5B007FDB514

    (除了提供 rest service.还提供了 remote shell ,和 mbean 方式,目的类似).


    原创出处:www.rapharino.com 作者:Rapharino IN,请继续关注Rapharino的博客.
  • 相关阅读:
    mongoDB安装配置
    linux-批量修改目录下后缀shell
    备份mysql的shell
    mysql_DML_索引、视图
    mysql_存储过程
    mysql_备份_mysqldump
    mysql_DCL_grant/revoke
    mysql_DML_select_子查询
    mysql_DML_select_union
    mysql_DML_select_聚合join
  • 原文地址:https://www.cnblogs.com/xmanblue/p/7010454.html
Copyright © 2011-2022 走看看