1, open SpringBoot tour

What is Spring Boot

Here Insert Picture Description
Spring Boot Spring team is designed to simplify the development process and framework to build Spring applications. The framework for third-party libraries for a simple default configuration, by Spring Boot applications built with little Spring configuration can be quickly up and running.

The benefits of using Spring Boot

Simple, fast and convenient.

Spring Web project to build a common general process:

  1. Configuring web.xml, the integration of Spring and SpringMvc
  2. Integration of Spring and Mybatis, Spring transaction configuration
  3. Configuring Log Files
  4. Tomcat deployment debugging

.......

A series of complicated configuration process, it is easy to miss out, even if just to build a simple crawling a page is stored in the mailbox database of small projects, from start to finish the configuration process must go again.

If you are using Spring Boot, the configuration is simplified a lot, just introduced several maven-dependent response, a few simple configuration can be quickly and easily build a Web project.

Open Spring Boot Tour

Construction of the line

  1. Access http://start.spring.io
  2. Select Maven Project, Java, Spring Boot 2.1.9 build, click Generate to generate the corresponding file. Project information simple configuration.
    Here Insert Picture Description
    Remarks:

Dependencies at by adding the appropriate dependency, will be automatically added when you build the project file, for example, if we want to do Web development, Web can be added as long as the dependent in.

  1. MyEclipse, Import -> Existing Maven Projects -> Next -> select the file after decompression folder -> Finsh!
    Here Insert Picture Description

    Construction of the project by Myeclipse

  2. File -> New -> Spring Starter Project New Project

  3. Basic information about configuration items
    Here Insert Picture Description
  4. The choice depends, such as Spring Web
    Here Insert Picture Description
  5. Finish OK, Spring Boot project build.

Tips:

导入或者Myeclipse构建项目有可能会出现pom.xml文件首行出现错误:Unknown pom.xml /demo line 1 Maven Configuration Problem。

解决方案

在pom文件中的 节点中加入<maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>然后右键项目进入Maven>Update Project...菜单点击,即可

项目结构解析

Here Insert Picture Description
Spring Boot 的基础结构共三个文件:

  • src/main/java 程序开发以及主程序入口:Application
  • src/main/resources 配置文件存放位置:application.properties
  • src/test/java 测试入口:ApplicationTests
  1. 在Application 右键run启动Spring Boot项目

生成的ApplicationApplicationTests类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。

Web开发

默认Tomcat启动项目

spring-boot-starter-parent指定了当前项目为一个Spring Boot项目,它提供了诸多的默认Maven依赖。

Spring Boot提供了许多开箱即用的依赖模块,这些模块都是以spring-boot-starter-XX命名的。比如要开启Spring Boot的web功能,只需要在pom.xml中配置spring-boot-starter-web即可:

  1. pom.xml中添加支持web的模块
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

因为其依赖于spring-boot-starter-parent,所以这里可以不用配置version。

pom.xml 文件中默认有两个模块:

  • spring-boot-starter :核心模块,包括自动配置支持、日志和 YAML,如果引入了 spring-boot-starter-web web 模块可以去掉此配置,因为 spring-boot-starter-web 自动依赖了 spring-boot-starter
  • spring-boot-starter-test :测试模块,包括 JUnit、Hamcrest、Mockito。
  1. 编写Controller层
package com.w3cjava.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
}

@RestController 相当于Spring中的@Controller和@ResponseBody组合使用的,直接以 json 格式输出。

  1. 启动主程序,打开浏览器访问http://localhost:8080/hello 结果界面如下,你已经可以愉快的开发Web项目了。
    Here Insert Picture Description

    配置Jetty启动项目

  2. 在pom.xml文件中找到spring-boot-starter-web,在其依赖中排除tomcat依赖

<!-- 支持web的模块依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 排除tomcat依赖 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  1. 添加jetty依赖
<!-- jetty依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
  1. 启动项目主程序,可见tomcat已经替换为jetty了。
    Here Insert Picture Description
  2. 访问 http://localhost:8080/hello 页面结果显示正常。

单元测试

模拟对 http://localhost:8080/hello 发送请求测试

package com.w3cjava;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.w3cjava.controller.HelloWorldController;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

    private MockMvc mvc;

    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
    }

    @Test
    public void getHello() throws Exception {
     mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Hello World")));
    }
}

热部署

  1. 添加spring-boot-devtools依赖
<!-- 热部署依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
  1. 启动主程序
  2. 在HelloWorldController中添加新的映射方法
@RequestMapping("/hello1")
public String index1() {
    return "Hello World1";
}   
  1. 直接访问 http://localhost:8080/hello1 正常
    Here Insert Picture Description

    spring-boot-maven-plugin插件

spring-boot-maven-plugin:能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Maven操作的可能。

Spring Boot Maven plugin的5个Goals

  • spring-boot:repackage,默认goal。在mvn package之后,再次打包可执行的jar/war,同时保留mvn package生成的jar/war为.origin
  • spring-boot:run,运行Spring Boot应用
  • spring-boot: start, in mvn integration-test phase, managing the application lifecycle Spring Boot
  • spring-boot: stop, in mvn integration-test phase, managing the application lifecycle Spring Boot
  • spring-boot: build-info, information file generating construct used build-info.properties Actuator

summary

In general terms, you can quickly build the project by Spring Boot, if you need to use a particular function, just add the corresponding dependencies and simple configuration items.

Source: 01.Spring-the Boot-Demo

Welcome to sweep the surface following the two-dimensional code concern " cosine plots " public micro signal
Here Insert Picture Description

In all things, hope and beauty

Guess you like

Origin www.cnblogs.com/cosecholand/p/11668325.html