zoukankan      html  css  js  c++  java
  • spring-in-action_day01

    前景说明:SpringInAction主要致力于SpringBoot为基础的讲解,尽可能多的使用SpringBoot,可以减少显行的配置,如xml配置,可以更加的专注于功能的实现。

    第一章:主要讲了如何使用sts根据创建一个简单的springboot项目,并对项目的内容作了介绍。

    一:使用sts创建springboot简单项目步骤

    1. Sts工具---file---new---Spring Starter Project
    2. 填写项目信息

        输入选项:

          name-项目名 ,

          groupgroupid

          ArtifactArtifactid(name同名最好)

          packet:主类所在的包包名

        还有几个默认选项:

          type :MavenProject 指创建maven项目

          Packagingjar ,指讲项目构建成一个可执行的jar文件。为什么不是war文件呢,打包为jar文件是基于云思维作出的选择,所有的java云平台都能执行jar文件,但不是所有的云平台都支持war文件

      3.选择需要的组件依赖

       

        这些组件根据我们的项目需要来进行选择

      4.Finish

      以上我们完成了一个最简单的springboot项目的创建,我们看看项目目录

    二:项目目录结构介绍

    从目录结构上来看是一个很标准的maven项目,对于几个特殊的文件和文件夹,做个介绍

      1. src/main/resources/staticstatic文件夹

    这里用来存放为浏览器提供服务的静态文件,包括图片、css文件等

      2.  src/main/resources/templatestemplates文件夹

    这个文件夹用来存放渲染到浏览器的模板文件,简单来说就是页面文件

      3. mvnwmvnw.cmd这是maven包装器脚本,我们即便没有安装maven,也是够创建maven项目

     

    三:pom文件

      1  <?xml version="1.0" encoding="UTF-8"?>
      2 
      3  
      4 
      5 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      6 
      7 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      8 
      9 <modelVersion>4.0.0</modelVersion>
     10 
     11 <parent>
     12 
     13 <groupId>org.springframework.boot</groupId>
     14 
     15 <artifactId>spring-boot-starter-parent</artifactId>
     16 
     17 <version>2.3.2.RELEASE</version>
     18 
     19 <relativePath/> <!-- lookup parent from repository -->
     20 
     21 </parent>
     22 
     23 <groupId>com.example</groupId>
     24 
     25 <artifactId>demo</artifactId>
     26 
     27 <version>0.0.1-SNAPSHOT</version>
     28 
     29 <name>demo</name>
     30 
     31 <description>Demo project for Spring Boot</description>
     32 
     33  
     34 
     35 <properties>
     36 
     37 <java.version>1.8</java.version>
     38 
     39 </properties>
     40 
     41  
     42 
     43 <dependencies>
     44 
     45 <dependency>
     46 
     47 <groupId>org.springframework.boot</groupId>
     48 
     49 <artifactId>spring-boot-starter-web</artifactId>
     50 
     51 </dependency>
     52 
     53  
     54 
     55 <dependency>
     56 
     57 <groupId>org.springframework.boot</groupId>
     58 
     59 <artifactId>spring-boot-starter-test</artifactId>
     60 
     61 <scope>test</scope>
     62 
     63 <exclusions>
     64 
     65 <exclusion>
     66 
     67 <groupId>org.junit.vintage</groupId>
     68 
     69 <artifactId>junit-vintage-engine</artifactId>
     70 
     71 </exclusion>
     72 
     73 </exclusions>
     74 
     75 </dependency>
     76 
     77 ..........其它依赖
     78 
     79 </dependencies>
     80 
     81  
     82 
     83 <build>
     84 
     85 <plugins>
     86 
     87 <plugin>
     88 
     89 <groupId>org.springframework.boot</groupId>
     90 
     91 <artifactId>spring-boot-maven-plugin</artifactId>
     92 
     93 </plugin>
     94 
     95 </plugins>
     96 
     97 </build>
     98 </project>

       1. parent标签

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

      我们的项目以spring-boot-starter-parent为父pom。这个父pom为我们提供了spring常用的一些项目依赖。它还有个varsion属性,这是springboot的版本。我们引入依赖不需要指定版本号,引入的依赖都会和这个版本相适应。也就是说我们不用对依赖的版本进行管理,再也不用担心版本冲突了

      2.依赖

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

        1)不需要指定版本号。

        2)这个依赖带了starter,这是springboot依赖,这种依赖的好处在于它是功能性的,会把它需要的一系列的依赖自动拉取进来。比如说spring-boot-starter-web依赖,他是web依赖,那么它会拉取一系列的web功能需要的依赖。简单来说,这么一个依赖,相当于引入了一批依赖

      3. 插件

    <build>
    
    <plugins>
    
    <plugin>
    
    <groupId>org.springframework.boot</groupId>
    
    <artifactId>spring-boot-maven-plugin</artifactId>
    
    </plugin>
    
    </plugins>
    
    </build>

        1)允许我们使用maven来运行项目

        2)确保所有的依赖打包到jar文件中

        3)它会在jar中生成一个manifest文件,将引导类声明为可执行的主类

     

    四:主类(引导类)介绍

      主目录下的主类DemoApplication.java,也叫引导类,这是自动创建的

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    
    public class DemoApplication {
    
    public static void main(String[] args) {
    
    SpringApplication.run(DemoApplication.class, args); 
      } }

      1.类上的注解@SpringBootApplication

        它是一个组合注解,实际上包含了3个注解

         * 1.@SpringBootConfiguration 将类声明为配置类

         * 2.@EnableAutoConfiguration 启动springboot自动配置

        * 3.@ComponentScan 启用组件扫描

      2.主方法里面SpringApplication.run(DemoApplication.class, args);

        *  它有两个参数,一个是配置类的class文件,第二个是命令行参数,

          现在传递给run方法的配置类是引导类,但是还可以传递其它配置类,但是传递引导类是最简洁和最典型的做法

        配置类:对于不能自动配置的组件,我们可以在引导类(它就是个配置类)中配置,但是最好的方法是单独在创建一个配置类来配置这些组件,也就是说配置类可以有多个

        命令行参数

          当你在Java命令行后面带上参数,Java虚拟机就直接du把他存放到了main方法中的zhi参数String数组里了,

          你可以运行下下面dao这个小程序就明白!!

    public class Test {
    
    public static void main(String[] args)
    
    {
    
    System.out.println(args[0]);
    
    System.out.println(args[1]);
    
    System.out.println(args[2]);
    
    }
    
    }

          用控制台编译: javac Test.java

          运行: java Test aaa bbb ccc

        主类的作用:主类,它会在JAR运行的时候被执行,调用run方法,开始初始化spring上下文,简单来说就是springboot项目的启动按钮。启动springboot项目。在sts工具中我们可以直接执行该main方法来启动项目。

    五:编写Controller

    package tacos.controller;
    
    import org.springframework.stereotype.Controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    
    public class FirstController {
    
    @GetMapping("/testhome") public String home(){
    
    return "/t1/home";  
    
    //返回视图名(包括路径),由于使用的@GetMapping注解,没有使用@ResponseBody注解,不是以json参数返回,而是会直接跳转返回的路径
    
    }
    
    }

      这是一个很简单的controller类,我们不需要去配置xml文件,不需要在xml文件里面配置web相关内容,比如说controller扫描,注解驱动等等,我们可以直接使用

      该类中,返回了一个跳转路径return "/t1/home";   对这个路径,做一个说明 这里的/ 是指 src/mian/template/,所以/t1/home是指src/mian/template/t1/home,也就是说静态资源的绝对路径是src/mian/static/下的

    六:模板文件

      静态文件home,实际是home.html,这里使用的是thymeleaf模板

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml" 
    
          xmlns:th="http://www.thymeleaf.org">
    
      <head>
    
        <title>Taco Cloud</title>
    
      </head>
    
      
    
      <body>
    
        <h1>Welcome to...</h1>
    
        <img th:src="@{/images/TacoCloud.png}"/>  
    
       </body>
    
    </html>

      注意img标签的写法,  img标签这里使用了thymeleafth:src属性和@{...}表达式,做图片引用

    到这里我们就完成了一个简单的springboot项目

    此时我们访问这个项目的这个controllertesthome接口: http://localhost:8080/testhome

    七:测试类介绍

    @SpringBootTest
    
    class DemoApplicationTests {
    
     
    
    @Test
    
    void contextLoads() {
    
    }
    
    }

       这是自动生成的测试类

    @SpringBootTest标识这是springboot测试类

    自己写一个测试类

    package tacos;
    
     
    
    import static org.hamcrest.Matchers.containsString;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
    
     
    
    import org.junit.Test;
    
    import org.junit.runner.RunWith;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
    
    import org.springframework.boot.test.context.SpringBootTest;
    
    import org.springframework.test.context.junit4.SpringRunner;
    
    import org.springframework.test.web.servlet.MockMvc;
    
     
    
    import tacos.controller.FirstController;
    
     
    
     
    
    @RunWith(SpringRunner.class)
    @WebMvcTest(FirstController.class) 
    public class HomeControllerTest { @Autowired private MockMvc mockMvc; @Test public void testHomePage() throws Exception { mockMvc.perform(get("/testhome")) //发起请求 .andExpect(status().isOk()) //期望得到http200 .andExpect(view().name("t1/home")) //期待得到为home视图 .andExpect(content().string( //期望包含Welcome to... containsString("Welcome to..."))); }

      

      注解:

    1. @RunWith(SpringRunner.class)

        这是Junit的一个注解,提供一个测试运行器来知道Junit来运行测试,可以想象成给Junit一个插件,以提供自定义的测试行为,这里使用的是Spring提供的一个测试运行期

       2.@WebMvcTest(FirstController.class)

        指定是针对FirstController的测试类,也可以不指定,直接使用@WebMvcTest

          1.它会将这个测试在springmvc应用的上下文中执行,在本例中,它会将FirstController注册到springmvc中,这样我们就可以向他发送请求了

            2.它也会为测试springmvc应用提供spring环境的支持

      MockMvc

        为何使用MockMvc?

         对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便,依赖网络环境等,所以为了可以对Controller进行测试,我们引入了MockMVC。

         MockMvc实现了对Http请求的模拟,能够直接使用网络的形式,转换到Controller的调用,这样可以使得测试速度快、不依赖网络环境,而且提供了一套验证的工具,这样可以使得请求的验证统一而且很方便。

     

  • 相关阅读:
    JavaWeb——Servlet开发3
    8.3.3
    8.3.2
    8.3
    8.2
    8.1
    7.3.9
    7.3.8.3
    7.3.8.2
    7.3.8.1
  • 原文地址:https://www.cnblogs.com/jthr/p/13415067.html
Copyright © 2011-2022 走看看