An introduction to SpringBoot's quick Build application approach

  • 2020-11-26 18:48:49
  • OfStack

First, the usage scenario of SpringBoot on Coding is introduced. Email services built using SpringBoot in Coding.

SpringBoot was chosen because it is more lightweight. In the usual Spring project, there were too many libraries to rely on and too many configurations to use for a program that only offered Email services. SpringBoot provides 1 of the common non-functional large project class features (such as embedded servers, security, metrics, health checks, externalizing configurations) that make it easier to deploy, such as Tomcat/Jetty directly (no need to deploy war packages separately)

1. The web built by Spring MVC and Spring Boot are different. Spring provides the ES23en-ES24en-ES25en-ES26en automatic configuration module.

2. Add the following dependencies


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

The default error page for SpringBoot

The default error page for Springboot is 1 called whitelable error page. You can create a new Controller in our project to map the error page as follows


package com.artbrain.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Administrator on 2016/6/16.
 */
@Controller
public class IndexController {
  @RequestMapping(value = "/")
  public String index()
  {
    return "Here, is a error page!";
  }
}

But an even better approach is to register 1 "/" controller, as shown below

Override the addViewControllers method in ES49en.java to register one viewController


 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
   registry.addViewController("/home").setViewName("home");
   registry.addViewController("/").setViewName("home");
   registry.addViewController("/profile").setViewName("profile");
 }

Engagement at the project structure level

springboot has one embedded tomcat, and its project structure is very different from JavaWeb application, which is traditionally packaged as war package. Specifically, the location of static files and page templates has changed. The original 1 series resources under src/main/webapp directory are now required to be placed under the corresponding subdirectory of src/main/resources. Specifically reflected as follows:

src/main/resources/static to hold all kinds of static resources, such as css, js static resources, etc

src/main/resources/templates to hold a template file, such as *. html

The alternative

If you want to continue to use the war package, you can keep the original project structure, but SpringBoot USES the package as 'jar'

Convention and customization at the SpringMVC framework level

spring-boot-starter-web is automatically configured by default with the following 1 necessary SpringMVC components:

Necessary viewResolver, such as ContentNegotiatingViewResolver and BeanNameResolver.

Register necessary bean such as Converter,GenericConverter and Formatter to the IoC container.

Add series 1 HttpMessageConverter to support Web requests and corresponding type conversions.

Automatic configuration and registration of MessageCodesConverter

We can register a new bean to replace SpringMVC components at any time.

Embedded Web container level conventions and customizations

By default, ES128en-ES129en-ES130en-ES131en USES embedded tomcat as the web container to provide external services. By default, tomcat USES the default port of 8080. Meanwhile, ES135en-ES136en-ES137en-ES138en provides the following optional configuration:

Replace the tomcat server

spring-boot-starter-jetty or ES148en-ES149en-ES150en-ES151en were introduced as alternatives

Change the default port of the web container

Change the configuration option to server.port =9000(in the ES160en.properties file for the springboot project, as shown in the sample code below)


spring.datasource.url=jdbc:mysql://localhost/spring_boot?autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=9000

springBoot provides a number of server. Prefixed configuration items for users to configure embedded web containers, such as:

server.port
server.address
server.ssh.*
server.tomcat.*

At the same time, Spring also allows us to directly customize the embedded web container example by registering one component of EmbeddedServletContainerCustomizer type in the IoC container.

conclusion

That's the end of this article's introduction to SpringBoot's approach to quickly building applications, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: