Spring Boot项目构建、测试、热部署、配置原理、执行流程(二)

SpringBoot项目构建、测试、热部署、配置原理、执行流程

一、项目构建

1.打开IDEA,建Maven项目。可选择webapp(web工程),也可不选直接创建也可以生成web工程,只是webapp最后生成的是war包,而不选则生成的是jar包;

2.在pom.xml中添加SpringBoot依赖,如下图:

<!--引入Spring Boot依赖,进行统一的版本依赖管理,下面的依赖不需再添加版本号-->
<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <verson>2.1.3.RELEASE</verson>
</parent>

<dependencies>
    <!--引入Web场景依赖启动器,自动导入SpringMVC框架相关的jar包,另外SpringBoot内嵌了tomcat、jetty等服务器,所以无需启动tomcat也可处理请求-->
    <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>           
    </dependency>
</dependencies>

3.启动编写主程序类(这种不常用,因为有局限性,只能加载单个类,重点了解原理,一般都是扫描注解)

......
@SpringBootApplication//标记该类为主程序启动类
public class testSpringBootRun{
    public static void main(String[] args) {
        SpringApplication.run(testSpringBootRun.class,args);
    }
}

4.创建一个用于Web访问的Controller

......
@RestController // 等价于 @Controller+@ResonseBody的共同作用   返回json字符串
public class HelloController{
    @GetMapping("/hello") // 等价于 @RequestMapping(RequestMethod.GET)
    public String hello(){
        return "hello Spring Boot";
    }
}

Spring Initializr方式构建SpringBoot项目

区别在于

1.选择了Spring Initializr方式创建

2.Maven插件在pom.xml中添加Maven依赖  ;

其余没有太多区别;

二、单元测试和热部署

I )单元测试

1.在pom.xml添加测试依赖启动器

......
<dependencies>
    <!--引入Web场景依赖启动器-->
    <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>           
    </dependency>
    <!--测试依赖启动器-->
    <dependency>
                <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-test</artifactId>   
               <scope>test</scope>        
    </dependency>
</dependencies>
......

2.编写单元测试类和测试方法

//......
@RunWith(SpringRunner.class) //测试运行器,并加载Spring Boot测试注解  在Spring里面是导入junit4
@SpringBootTest  //标记单元测试类,并加载项目的上下文环境ApplicationContext
public class testSpringBootRun{
    @Autowired
    private HelloController helloController;
    @Test
    public void helloController(){
        String hello=helloController.hello();
        System.out.println(hello);
    }
}

II)热部署

       说明:在开发过程中不建议写完所有的代码再测试,而是写一点测试一点,与此同时,需要重启服务,

这过程加载时间过长,降低开发效率,而热部署无需手动重启项目能自动构建和编译,这提高了开发和

测试的效率。

      步骤如下

1.在pom.xml中添加依赖,即Spring-boot-devtools热部署启动器

//...
 <!--引入热部署依赖-->
    <dependency>
                <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-devtools</artifactId>  
    </dependency>
//...

2.热部署设置

      File---->Setting---->Build,Execution,Deployment---->Compiler---->勾选Build project automatically---->apply---->OK

三、配置原理、执行流程

       主要讲三点:1.依赖管理  2.自动配置  3.执行流程

1.依赖管理

       有两个重要配置,spring-boot-starter-parent、spring-boot-starter-web;

<!--引入Spring Boot依赖,进行统一的版本依赖管理,下面的依赖不需再添加版本号-->
<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <verson>2.1.3.RELEASE</verson>
</parent>

<dependencies>
    <!--引入Web场景依赖启动器,自动导入SpringMVC框架相关的jar包,另外SpringBoot内嵌了tomcat、jetty等服务器,所以无需启动tomcat也可处理请求-->
    <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>           
    </dependency>
</dependencies>

spring-boot-starter-parent这个依赖封装了非常多的工具包的版本号,点进去可以看到底层源文件,所以一般情况下不需要在依赖中添加版本号。

spring-boot-starter-web这个依赖封装了一些非常多的依赖,点进去可以看到底层封装的依赖,包含web开发的很多的依赖,也包含了tomcat服务

器的封装,所以无需添加tomcat服务器。

2.自动配置

......
@SpringBootApplication//标记该类为主程序启动类
public class testSpringBootRun{
    public static void main(String[] args) {
        SpringApplication.run(testSpringBootRun.class,args);
    }
}

        在启动一个类的时候,在main方法上添加@SpringBootApplication来启动的,该注解能自动扫描Spring组件,并自动配置SpringBoot。该注

解是下面三个注解的组合注解;即@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan这三个注解;

作用分别是

               @ComponentScan作用:扫描启动类的包位置;

@SpringBootConfiguration作用:扫描配置类;

@EnableAutoConfiguration作用:环境的封装,开启自动配置;

 

3.执行流程

SpringApplication.run()是如何启动SpringBoot项目的?

          点开源码看,分别分为两步:1.初始化 2.项目的启动

I)初始化

         判断当前项目类型,应用的初始化器、监听器、项目启动类的设置。

II)项目启动

         获取并允许监听、准备项目环境、上下文装配并启动允许自定义执

行器、上下文等,最后项目结束。

成年人的世界没有那么多的童话,也没有那么多的逆袭。
原文地址:https://www.cnblogs.com/shijinglu2018/p/12089186.html