First, let’s introduce the usage scenarios of SpringBoot on Coding. Email service built using SpringBoot in Coding.
The reason I chose SpringBoot is that it is more lightweight. In the usual Spring projects, there are too many libraries that depend on and too complicated configurations, so it is a big deal to use this program that only provides email services. SpringBoot provides some non-functional, common large-scale project-like features (such as embedded servers, security, metrics, health checks, external configurations) making it more convenient for us to deploy, such as directly embedded Tomcat/Jetty (no need to deploy war packages separately)
1.The web construction methods that come with Spring MVC and Spring Boot are different. Spring provides spring-boot-starter-web automatic configuration module.
2. Add the following dependencies
<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 a new controller like the following in our project to implement the mapping of the error page.
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 more general approach is to register a "/" controller, the code is as follows
Rewrite the addViewControllers method in Application.java and 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 agreement
Springboot has a tomcat embedded in it. Its project structure is very different from that of traditional JavaWeb applications packaged as war packages. The storage location of static files and page templates has changed. It used to be a series of resources placed in the src/main/webapp directory. Now it is required to be placed in the corresponding subdirectory of src/main/resources. Specifically reflected in:
src/main/resources/static is used to store various static resources, such as css, js and other static resources
src/main/resources/templates is used to store template files, such as *.html
Available ways
If you want to continue using the war package, you can maintain the original project structure, but the packaging method used by SpringBoot is 'jar'
SpringMVC framework level agreements and customization
Spring-boot-starter-web automatically configures the following necessary components of SpringMVC by default:
Necessary viewResolvers, such as ContentNegotiatingViewResolver and BeanNameResolver.
Register necessary Converter, GenericConverter and Formatter beans into the IoC container.
Add a series of HttpMessageConverters to support web requests and corresponding type conversions.
Automatically configure and register MessageCodesConverter
We can register a new bean at any time to replace SpringMVC's components.
Conventions and customization at the embedded web container level
Spring-boot-starter-web uses embedded tomcat as a web container to provide services to the outside world. The default port of tomcat is 8080. At the same time, spring-boot-starter-web provides the following optional configurations:
Replace tomcat server
Introduce spring-boot-starter-jetty or spring-boot-starter-undertow as an alternative
Change the default port of the web container
Change the configuration option to: server.port=9000 (changed in the application.properties file of the springboot project. The following example code also gives the configuration method of jdbc)
spring.datasource.url=jdbc:mysql://localhost/spring_boot?autoReconnect=truespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driversserver.port=9000
springBoot provides many server. prefixed configuration items users 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 embedded web container examples. We can register an EmbeddedServletContainerCustomizer type component in the IoC container to customize embedded web containers.
Summarize
The above is all the content of this article about how to quickly build SpringBoot applications. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!