SpringBoot: How to quickly build an application. springboot Application

Source: Internet
Author: User

SpringBoot: How to quickly build an application. springboot Application

First, we will introduce the use cases of SpringBoot in Coding. The e-mail service built using SpringBoot in Coding.

SpringBoot is more lightweight. In Spring projects, there are too many dependent libraries and the configuration is too complicated. It is a big deal to use this e-mail service-only program. SpringBoot provides some non-functional and common large-scale project features (such as embedded servers, security, metrics, health check, and external configuration), making it easier for us to deploy, for example, you can directly embed Tomcat/Jetty (you do not need to deploy the war package separately)

1. The web build methods provided by Spring MVC and Spring Boot are different. Spring provides the spring-boot-starter-web Automatic Configuration Module.

2. Add the following dependency

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

SpringBoot default error page

Springboot's default error page is a whitelable error page. You can create the following Controller in our project to map error pages.

package com.artbrain.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * Created by Administrator on 2016/6/16. */@Controllerpublic class IndexController {  @RequestMapping(value = "/")  public String index()  {    return "Here, is a error page!";  }}

But the general practice is to register a "/" controller. The Code is as follows:

Override the addViewControllers method in Application. java to register a viewController

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

Project structure layer conventions

Springboot is embedded with a tomcat, and its project structure is very different from that of traditional Java Web applications packaged as war packages. The storage location of static files and page templates has changed, A series of resources originally stored in the src/main/webapp directory must be placed under the corresponding subdirectories of src/main/resources. Specific embodiment:

Src/main/resources/static is used to store various static resources, such as css and js.

Src/main/resources/templates is used to store template files, such as *. html

Available Methods

If you want to continue using the war package, you can maintain the original project structure, but SpringBoot uses the 'jar' packaging method'

SpringMVC framework-level conventions and customization

Spring-boot-starter-web automatically configures the following components necessary for spring MVC by default:

Necessary viewResolver, such as ContentNegotiatingViewResolver and BeanNameResolver.

Register necessary Converter, GenericConverter, Formatter, and other beans to the IoC container.

Add a series of HttpMessageConverter to Support Web requests and corresponding type conversion.

Automatically configure and register MessageCodesConverter

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

Embedded Web Container layer conventions and customization

By default, Spring-boot-starter-web uses embedded tomcat as the web container to provide external services. The default port of tomcat is 8080, spring-boot-starter-web also provides the following optional configurations:

Replace tomcat server

Use spring-boot-starter-jetty or spring-boot-starter-undertow as an alternative.

Change the default port of the web Container

The configuration change option is server. port = 9000 (changed in the application. properties file of the springboot project, and the jdbc configuration method is also provided in the following sample code)

spring.datasource.url=jdbc:mysql://localhost/spring_boot?autoReconnect=truespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverserver.port=9000

SpringBoot provides many server-Prefix configuration items for users to configure embedded web containers, such:

Server. port
Server. address
Server. ssh .*
Server. tomcat .*

At the same time, Spring also allows us to customize embedded web Container examples. We can register an EmbeddedServletContainerCustomizer component in the IoC container to customize embedded web containers.

Summary

The above is all the content about spring boot's rapid application building method. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.