Welcome everyone

Spring Boot 介绍篇

java 汪明鑫 897浏览 0评论

 

Spring分析

Spring为企业级Java开发提供了一种相对简单的方法,最核心的功能就是依赖注入和面向切面编程,降低代码,加快开发速度,简化开发。

Spring 可以和很多很多优秀的开源框架整合。

但是Spring也有很多缺点,比如说使用Spring,需要写大量的配置文件,配置文件相当臃肿,哪怕后来出现了基于注解的开发,但是

依然要在配置上花费很多时间。

 

早在很久前,SSM便干掉SSH一统江湖,Strusts存在安全的问题,Mybatis比Hibernate更轻量级,可以由程序员自己编写Sql

Spring MVC 还是Spring的一部分。

技术总在更新换代,开发人员怎么能减少配置,减少编码,全力关注于业务逻辑,

于是乎,Spring Boot横空出世,逐渐取代了SSM的霸主地位。

虽然SSH基本上没怎么见了,包括我的几次实习,要么SSM+jsp/freemarker,要么Spring Boot + Vue/React

 

 

Spring Boot 前言

SpringBoot对Spring的缺点进行的改善和优化

创建一个Spring Boot项目,零配置,就能跑起来

约定大于配置

可以直接使用内嵌容器

Spring Boot提供了一种快速使用Spring的方式,相当之给力

 

 

Spring Boot自动配置

@SpringBootApplication 标注SpringBoot的启动类,具备多种功能

 

@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 {
    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    Class<?>[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};
}

 

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

 

@SpringBootConfiguration等同于一个@Configuration  配置类,也是一个组件

@EnableAutoConfiguration SpringBoot自动配置功能开启

 

再查看注解@EnableAutoConfiguration

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

 

注意这句话
@Import({AutoConfigurationImportSelector.class})
导入了AutoConfigurationImportSelector类

 

再看下这个类

找到了一个这样的方法

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        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;
    }

虽然看不大懂,但是看到下面有一个断言,写到 META-INF/spring.factories,

所以猜测应该时去找个文件加载什么东西

 

全局搜索一哈这个文件

 

spring.factories

比较多,只粘贴过来一部分

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener

# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition


........

 

 

都是一些有关自动配置的配置信息

仔细观察可以看到配置文件存在大量的以Configuration为结尾的类名称,这些类就是存有自动配置信息的类,
SpringApplication在获取这些类名后再加载

 

会给容器导入很多的自动配置类

免去我们手动编写配置注入功能组件

 

看下
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\

 

@EnableConfigurationProperties(ServerProperties.class) 代表加载ServerProperties服务器配置属性类

 

 

prefix = “server” 表示SpringBoot配置文件中的前缀,SpringBoot会将配置文件中以server开始的属性映射到该类
的字段中

 

这就是为什么我们在application.properties配置server.port=8081可以生效

 

是不是感觉有点牛逼,看的很蒙蔽的

 

Spring Boot 起步依赖

在pom文件中,看到

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

spring-boot-starter-parent进去看下

 

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
    </parent>

又看到parent了,说明他还有父亲(可以这样理解)

再点进去瞅一瞅

 

 

这里一部分坐标的版本、依赖管理、插件管理已经定义好,

所以SpringBoot工程继承spring-boot-starter-parent后已经具备一些配置了。

也验证了spring boot的约定优于配置的思想。

起步依赖的作用就是进行依赖的传递。

 

再看下spring-boot-starter-web

在pom文件找到这个依赖

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

点进去

 

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starters</artifactId>
    <version>2.1.3.RELEASE</version>
  </parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.1.3.RELEASE</version>
  <name>Spring Boot Web Starter</name>
  <description>Starter for building web, including RESTful, applications using Spring
		MVC. Uses Tomcat as the default embedded container</description>
  <url>https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>https://spring.io</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>http://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web</connection>
    <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web</developerConnection>
    <url>http://github.com/spring-projects/spring-boot/spring-boot-starters/spring-boot-starter-web</url>
  </scm>
  <issueManagement>
    <system>Github</system>
    <url>https://github.com/spring-projects/spring-boot/issues</url>
  </issueManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.3.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.1.3.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.1.3.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.14.Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.5.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

 

spring-boot-starter-web将web开发要使用的
spring-web、spring-webmvc等坐标进行了“打包”,

这样我们的工程只要引入spring-boot-starter-web起步依赖的
坐标就可以进行web开发了,相当于引入了在spring-boot-starter-web引入了依赖,

同样体现了依赖传递的作用。

 

Spring Boot将所有功能场景都抽取出来,做成了一个个的starters(启动器)

只用在项目里引用这些starter,相关场景的所有依赖都会引入进来

 

Spring Boot 热部署

修改类或者页面时,每次修改后都是需要重新启动才生效,非常麻烦

可以配置热部署,修改后就不用重启项目,自动编译了

 

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

 

还需要修改几个配置

 

Shift+Ctrl+Alt+/,选择Registry

 

 

 

修改项目启动图案

生成图案地址 : http://patorjk.com/software/taag/#p=display&f=3D-ASCII&t=Xinye

 

 

 

 

项目启动,启动图案就变成了自己设定的图案了

 

 

转载请注明:汪明鑫的个人博客 » Spring Boot 介绍篇

喜欢 (0)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz