关于java:JT项目01环境搭建及SpingBoot启动原理说明

4次阅读

共计 6947 个字符,预计需要花费 18 分钟才能阅读完成。

1. 我的项目环境搭建

1.1 JDK 配置

1.1.1 JDK 环境阐明

1.1.2 JDK 环境变量配置

1.2 Maven 配置

1.2.1 私服镜像配置

<mirror> 
<id>aliyun</id> 
<name>aliyun for maven</name> 
<mirrorOf>*</mirrorOf> 
<url>https://maven.aliyun.com/repository/public</url> 
</mirror>

1.2.2 本地仓库地位

1.3 STS 配置

1.3.1 配置字符集编码格局

1.3.2 查看 JDK 配置

1.3.3 引入 maven

1). 配置 maven 环境

2). 编辑 maven 配置文件

1.4 构建 SpringBoot 入门我的项目

1.4.1 创立我的项目


2 我的项目概述

3.SpringBoot 框架高级阐明

3.1 parent 标签的作用

<!--1.jar 包品种繁多 A.jar 1.0 版本 B.jar 2.0 版本 C.jar 3.0 版本 A.jar 2.0 版本 B.jar 2.0 版本 C.jar 3.0 版本 
晚期的 jar 包版本 可能呈现 jar 包抵触的问题. 所有采纳 parent 标签的形式 对立定义了版本号 
由官网本人进行测试, 将容许稳固的版本对立的治理. -->
<parent> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-parent</artifactId> 
<version>2.3.2.RELEASE</version> <relativePath />
</parent>

3.2 对于 maven 插件的阐明

<!-- maven 我的项目指定的插件配置 该插件次要负责 maven 我的项目相干操作 打包 /test/clean/update
        等相干 maven 操作 注意事项: 但但凡 maven 我的项目则必须增加 
        插件. 否则未来我的项目部署必然出错 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3.3 对于 Maven 属性的配置

<!-- 以后 maven 配置信息 -->
    <properties>
        <java.version>1.8</java.version>
          <!-- 定义 maven 插件版本  -->
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
              <!-- 我的项目打包时, 跳过测试类打包 -->
        <skipTests>true</skipTests>
    </properties>

3.4 对于 Maven 依赖阐明

3.4.1 依赖与本地仓库的关系

<!--maven 的依赖 利用坐标的模式治理 jar 包 本地仓库之间有什么关系呀?  -->
<dependency>
<!-- 组 ID, 公司域名倒写.  -->
     <groupId>org.springframework.boot</groupId>
     <!-- 具体项目名称  -->
     <artifactId>spring-boot-starter-web</artifactId>
     <!-- 版本号 被 parent 标签提前定义 -->
     <!-- <version>2</version> -->
</dependency>

本地仓库内容

规定阐明
如果当前下载 jar 包时, 遇到 maven 依赖异样. 则首先查问本地仓库中是否有该 jar 包的残缺版本.
如果发现 jar 包文件下载不齐全, 则须要删除之后从新下载.

3.4.2 jar 包与 jar 包之间的依赖传递性阐明

1. 阐明: maven 具备 jar 包的依赖性
例如: A.jar—->B.jar ——> C.jar
只有导入 A.jar 那么 b / c 都会主动的实现依赖

2). 查看 POM.xml 文件
阐明: 因为 maven 加载 jar 包之后还会去加载该 jar 包文件的 POM 文件, 如果该 POM 文件中依赖了其余的 jar 包, 那么 maven 也会主动的进行加载.

4. IDEA 创立 SpringBoot 我的项目

3.1 创立 maven 我的项目

3.1.1 增加插件之后, 重启 IDEA 即可

3.1.2IDEA 创立 SpringBoot

1). 筛选 SpringBoot

2). 编辑模块内容

3). 抉择 jar 包依赖

3.2 手动创立 SpringBoot 我的项目

3.2.1 创立 maven 我的项目

3.2.2 指定项目名称

3.2.3 编辑 POM.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.jt</groupId>
 <artifactId>sprongboot_demo2</artifactId>
 <version>1.0-SNAPSHOT</version>
 <!--1.jar 包品种繁多
 A.jar 1.0 版本 B.jar 2.0 版本 C.jar 3.0 版本
 A.jar 2.0 版本 B.jar 2.0 版本 C.jar 3.0 版本
 晚期的 jar 包版本 可能呈现 jar 包抵触的问题.
 所有采纳 parent 标签的形式 对立定义了版本号  由官网本人进行测试,
 将容许稳固的版本对立的治理.
 --> <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.3.4.RELEASE</version>
 <relativePath/> </parent>
 <!-- 开箱即用:SpringBoot 我的项目只须要引入大量的 jar 包及配置, 即可领有其性能.
 spring-boot-starter 领有开箱即用的能力.
 maven 我的项目中依赖具备传递性.
 A 依赖 B 依赖 C 我的项目   导入 A bc 会主动依赖
 -->
 <properties>
 <java.version>1.8</java.version>
 <!-- 定义 maven 插件版本 -->
 <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
 <!-- 我的项目打包时, 跳过测试类打包 -->
 <skipTests>true</skipTests>
 </properties>
 <dependencies> <!--2maven 的依赖 利用坐标的模式治理 jar 包 本地仓库之间有什么关系呀?  -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency> <!-- 组 ID, 公司域名倒写.  -->
 <groupId>org.springframework.boot</groupId>
 <!-- 具体项目名称 -->
 <artifactId>spring-boot-starter-test</artifactId>
 <!-- 版本号 被 parent 标签提前定义 -->
 <!-- <version>2</version> --> <scope>test</scope>
 <exclusions> <exclusion> <groupId>org.junit.vintage</groupId>
 <artifactId>junit-vintage-engine</artifactId>
 </exclusion> </exclusions> </dependency> <dependency> <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 </dependency> </dependencies> <!--3 maven 我的项目指定的插件配置 该插件次要负责 maven 我的项目相干操作 打包 /test/clean/update
 等相干 maven 操作 注意事项: 但但凡 maven 我的项目则必须增加
 插件. 否则未来我的项目部署必然出错 -->
 <build>
 <plugins> <plugin> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin> </plugins> </build></project>

3.2.4 编辑 POM.xml 配置文件

package com.jt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication// 标识我是一个 springboot 我的项目
public class SpringBootRun {
    /**
 * main 办法是 java 程序的惟一入口
 * @param args
 */
 public static void main(String[] args) {
        // 加载 @SpringBootApplication// 标识我是一个 springboot 我的项目
 SpringApplication.run(SpringBootRun.class,args);
 }
}

3.2.5 编辑测试 Controller

package com.jt.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloComtroller {@RequestMapping("/hello")
    public String hello(){return "hello";}
}

4.SpringBoot 启动原理阐明

4.1 需要阐明

1.SpringBoot 启动是如何流转的.
2. 什么叫做开箱即用,springBoot 中如何实现开箱即用的.

4.2 “ 开箱即用 ” 阐明

如果须要引入第三方的 ” 工具 / 性能 ”, 只须要通过大量的配置 / 或者不做任何配置. 则就能够应用该性能.

4.2.1 对于启动项的阐明

 <!-- 间接的依赖项 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <!--springBoot 启动项 外部的 jar 包文件曾经将配置筹备实现, 用户
            只须要导入 jar 包就能够获取其性能.
            starter-web  间接引入了 SpringMVC 框架
            传统 mvc 框架配置:
                    1. 导入特定的 jar 包及版本.
                    2. 编辑 web.xml 配置 dispatcherServler  拦挡门路
                    3. 配置 mvc 的配置文件  包扫描  视图解析器....

            -->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

4.3 SpringBoot 程序启动加载过程

4.3.1 元注解阐明:

@Target(ElementType.TYPE) 对哪个元素无效 对类无效
@Retention(RetentionPolicy.RUNTIME) 什么时候起作用 运行期无效
@Documented 是否反对生成文档
@Inherited 该注解是否能够被继承.

4.3.2 SpringBootConfiguration 阐明

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

4.3.3 excludeFilters 阐明

阐明: 在 springboot 容器启动时, 不须要加载某些过滤器. 因为 parent 标签中定义了所有的关联的 jar 包文件信息. 则启动时有可能导致意外的产生, 所有须要提前剔除.
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

4.3.4 @AutoConfigurationPackage

阐明: 当程序启动时, 会依据主启动类的包门路, 扫描其子孙包, 所以 springboot 当前写代码时应该在其子孙包下编辑.

4.3.5 AutoConfigurationImportSelector

阐明: 该配置中外部集成了所有的 SpringBoot 中的选择器. 这些选择器的次要的工作就是 查看是否有本人选择器所治理的启动项的配置. 如果发现该启动项, 那么选择器就会执行该启动项, 从而实现了开箱即用的操作.

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration matched:
      - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration' (OnClassCondition)
      - DispatcherServlet Registration did not find servlet registration bean (DispatcherServletAutoConfiguration.DispatcherS
   ServletWebServerFactoryAutoConfiguration#tomcatServletWebServerFactoryCustomizer matched:
      - @ConditionalOnClass found required class 'org.apache.catalina.startup.Tomcat' (OnClassCondition)
      .......

Negative matches:
-----------------

 
   R2dbcTransactionManagerAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.springframework.data.r2dbc.connectionfactory.R2dbcTransactionManager' (OnClassCondition)

   RSocketMessagingAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'io.rsocket.RSocketFactory' (OnClassCondition)
      Did not match:
     .........
Exclusions:
-----------

    None


Unconditional classes:
----------------------

    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

    org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration

    .........
正文完
 0