Spring Boot summary (2) — project rapid construction of actual combat

The first section gives a brief overview of Spring and Spring MVC, paving the way for Spring Boot. This article begins with a summary of Spring Boot.

The birth of Spring Boot

With the popularity of dynamic languages (Ruby, Groovy, Scala, node.js), Java is becoming more cumbersome. Configuration variety, deployment complex, third party technology integration difficult, become criticized.
so SpringBoot came into being. It uses the concept of “habit is better than configuration” to make the project run quickly. (built in custom configuration so you don’t have to configure manually)

Spring Boot core functionality

1. jar package independently run
only through Java -jar xx.jar

Spring Boot 2. embedded servlet container can choose embedded Tomcat, Jetty or Undertow a Spring Boot project. We do not need to deploy the project
3. provides starter simplified Maven configuration
offers a range of starter POM to simplify the dependency of Maven loaded by war package, you can directly use spring-boot-starter-web to quickly build project
4. automatic configuration Spring
Spring Boot according to the class path jar package, class, class jar automatic configuration bean in the packet, greatly reduce configuration.
5. quasi production application monitoring
HTTP, SSH Spring Boot and telnet on the run time of project monitoring
6. code generation and XML configuration through
to realize configuration based on code comments

Spring Boot quick build

1. build through start.spring.io

Open the browser login http://start.spring.io/, you can quickly build a maven management Spring Boot project.

Spring Boot summary (2) --- project rapid construction of actual combat
start.spring.io


界面如上图所示,设定我们自己项目的Group,Artifact,以及一些我们项目所要用的依赖,如Web、JPA、Thymeleaf等,点击 Generate Project 即可下载项目。这些依赖会被自动配置到pom文件中。下载后我们用自己的IDE打开这个基于maven 的项目,即可看到文件目录和框架。

Spring Boot summary (2) --- project rapid construction of actual combat
file directory


Yes! That’s it!!! Two points, less than 1 minutes to complete, before using Spring, MVC and other frameworks, it takes a long time to configure and directory building tasks! And almost all of the dependencies we’re going to use have been successfully added when building items, without having to add one by one!

2. build with IDEA

IDEA as the best use of Java engineering development tools, you can directly build Spring Boot project.
new project -> select Spring Initializr -> fill in the item information -> select the technical -&gt used for the project; fill in the project name -> generate successfully.

Spring Boot summary (2) --- project rapid construction of actual combat
Spring Initializr
Spring Boot summary (2) --- project rapid construction of actual combat
fill in item information
Spring Boot summary (2) --- project rapid construction of actual combat
selects the technology used in the project
Spring Boot summary (2) --- project rapid construction of actual combat
fills in the name of the project

Similarly, within 1 minutes, you can build a Spring Boot project. Visible, Spring Boot fast build characteristics of how powerful

Run Hello World

In the new project directory, we can have an entry class for the artifactId+Application
naming rule under the root directory of the project. Here I am DemoApplication.
to facilitate the demonstration, we do not create a new controller class. We simply write simple code in the entry class, as follows:

Package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication @EnableAutoConfiguration (exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class DemoApplication {@RequestMapping ("/") String (index) {return "H "Ello World!";} public, static, void, main (String[], args) {SpringApplication.run (DemoApplication.class, args)}}

Code interpretation:
[email protected]: the core annotation for Spring Boot, used to turn on automatic configuration. Methods:
2.main
3.RequestMapping project started entrance (“/”): root address intercept processing
request [email protected]:spring boot will start automatically into the data source and configure the JPA, because there is no relevant configuration information about the dataSource project, dataSource bean when the spring was created because of the lack of relevant information will be wrong. Currently, hello world is added to the @EnableAutoConfiguration class to prevent spring boot from automatically injecting dataSource, bean, and JPA into the Application class for project launch demos

Run the project through the MVN spring-boot:run command or the right mouse button -> run “DemoApplication”, and the console finds the project running successfully.

Spring Boot summary (2) --- project rapid construction of actual combat
right mouse button run

Enter localhost:8080 in the browser to see the results of the run.

Spring Boot summary (2) --- project rapid construction of actual combat
localhost:8080

At this point, the Spring Boot Hello World project has been successfully demonstrated. Compared with Spring MVC, the configuration mode of annotations without XML configuration is more convenient, faster and more efficient, and greatly improves the development efficiency.