您的位置:首页 > 编程语言 > Java开发

Spring Boot 概念知识

2017-06-14 18:52 232 查看

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

Spring Boot Induction

发表于 2016-10-29 | 分类于 coder | | 阅读次数 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 构建项目

123456789101112131415161718192021222324252627282930
<!-- 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>

编写代码

12345678910111213
@RestController@EnableAutoConfigurationpublic 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生命周期管理功能.





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

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

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

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

启动 自动配置流程

SpringBootApplication注解中带有EnableAutoConfiguration注解

12345678910111213141516171819202122232425
@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 {};}
EnableAutoConfiguration 为容器注册了一个EnableAutoConfigurationImportSelector对象.它则是自动配置的核心类.

EnableAutoConfigurationImportSelector实现了ImportSelector的selectImports方法:

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

1234567891011
# Auto Configureorg.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


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

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

容器初始化完成.(额外会提供点周边服务,如 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
配置使用一个特别的顺序进行合理的配置和覆盖.允许开发者通过配置服务,静态文件,环境变量,命令行参数,注解类等,用以外化配置.覆盖顺序如下:

命令行参数
来自于 java:comp/env 的 JNDI 属性
操作系统环境变量
在打包的 jar 外的配置文件(包括 propertis,yaml,profile变量.)
在打包的 jar 内的配置文件(包括 propertis,yaml,profile变量.)
@Configuration 类的@PropertySource 注解
应用框架默认配置
application.properties和application.yml文件能放在以下四个位置。亦拥有覆盖顺序(高 - 低)

外置,在相对于应用程序运行目录的/config子目录里。
外置,在应用程序运行的目录里。
内置,在config包内。
内置,在Classpath根目录。
每个模块都有自己的配置.在此不再赘述.

Actuator

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









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

原创出处:www.rapharino.com 作者:Rapharino IN,请继续关注Rapharino的博客.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: