Spring Boot Best Practices (i) Quick start

Source: Internet
Author: User
Tags spring initializr spring boot actuator

First, about spring Boot

Before we begin to understand spring boot, we need to look at spring, because the spring boot is related to spring, spring boot is a product of spring to a certain extent, but not a substitute for spring, Spring boot is designed to make it easier for programmers to use spring. Speaking of which, some people may be confused, what is spring and spring boot have what kind of connection?

1.Spring history

Before we get started, let's take a look at Spring,spring's predecessor, INTERFACE21, which was originally designed to solve the cumbersome and bloated EJB development problem, provided another simple and practical solution for the Java EE, and released the spring in March 2004. 1.0 The official version, the Java industry has aroused widespread concern and thermal evaluation, since spring in the Java World Irresistible force quickly became popular, a bright star in the Java community, so far irreplaceable, and once became the real standard in the development of the EE, and his founder Rod Johnson is also famous after fame, fame and fortune, now is a good angel investors, embarked on the pinnacle of life.

2.Spring Boot Birth

Now that spring is so good, why is spring Boot?

As spring grows more and more hot, spring slowly from a small and fine framework into a broad chatty framework, on the other hand with the development of new technologies, such as the rise of Nodejs, Golang, Ruby, let Spring gradually look cumbersome, A large number of tedious XML configurations and third-party consolidation configurations have made spring users miserable, and a solution is urgently needed to address these issues.

At this juncture spring boot came into being, 2013 spring boot began development, April 2014 Spring Boot 1.0 officially released, spring Boot was born in the industry's attention, many individuals and enterprises began to try, With the launch of Spring Boot 2.0, the spring boot has been brought to the public's eyes again, and more and more medium and large enterprises have used spring boot to the formal production environment. It is worth mentioning that spring is also the official spring boot as the primary promotion project, put on the official website first.

3.Spring Boot Introduction

Spring boot is a new framework provided by the pivotal team designed to simplify the initial construction and development of spring applications. The framework uses a specific approach to configuration, which eliminates the need for developers to define boilerplate configurations. Spring Boot is a framework that integrates many pluggable components (frameworks), embedded with tools such as Tomcat, Jetty, and so on, to allow developers to quickly build and develop.

4.Spring Boot Features
    • Build the project quickly, a few seconds can be completed;
    • Make the test simple, built-in JUnit, Spring Boot test and other testing framework, easy to test;
    • Spring boot makes configuration simple, Spring boot core concept: The agreement about the configuration, contract a certain naming specification, you can do without configuration, you can complete the function development, such as model and table name consistent can not be configured, directly to the CRUD (increase the deletion of check) operation, Only when the table name and model are inconsistent, the configuration name can be;
    • Embedded containers, eliminating the cumbersome configuration of Tomcat;
    • Easy to monitor and use the Spring Boot Actuator component to provide system monitoring of your application to see the details of your application's configuration;
Second, the development environment

Spring Boot 2.0.4

JDK 1.8

Ide:intellij Idea 2018.2

Note: The Spring Boot 2.x requires Java8 and above and no longer supports JAVA6, Java7.

Iii. Development Tools IntelliJ idea Vs MyEclipse

The choice of development tools is for everyone to go through and choose, if you are an old hand can ignore this section.

Once upon a time, Eclipse and MyEclipse ruled the Java user, and now, years and months, I used MyEclipse 6.0 to the present MyEclipse 2017 I didn't feel any change, or the same skin, the same layout, The same way the card takes off, even after you've optimized a bunch of configurations in accordance with Google's guidelines, you'll see little effect.

This reminds me of the once brilliant Nokia, so far it still does not know what they have done wrong, but the time of day when a thing can not keep pace with the changing times, it will be replaced by new more suitable things. I have to say that I used to be a loyal lover of MyEclipse, and it is difficult for a person to accept something for a habit, and usually change it.

However, to this day, I have to admit that IntelliJ idea is much better than myeclipse in terms of appearance to run speed, especially for spring boot support, so IntelliJ is the best choice.

Iv. Description of Spring boot version number

Spring boot version selection is generally the case, such as:

What does the English language after the version number mean?

Specific meanings, as shown in the following article:

    • SNAPSHOT: Snapshot version, indicating the development version, may be modified at any time;
    • M1 (Mn): M is the abbreviation of milestone, which is the milestone version;
    • RC1 (RCn): RC is the abbreviation for release candidates, which is the release preview version;
    • Release: Official version, or may not have any suffix also indicates the official version;
V. Spring Boot Project creation

The Spring Boot project is created in two ways:

    • Way one: Visit the website build project, download to the Local Import development tool
    • Method Two: Use the idea (IntelliJ idea abbreviation) tool to create the initialization using spring INITIALIZR (recommended)

Note: According to the "Development tools" section above, using idea is the most appropriate language for spring boot development, so everything in this section is done using idea.

Way one: visit Site Creation Project
    1. Visit URL: https://start.spring.io/
    2. To configure the project information, click the Generate Project button to build the project, such as: where group represents the organization identifier, corresponds to the structure of the Java package directory, and Arifact represents the project identifier, which is the project name, which is the root directory name.
    3. Unzip the folder and click on the idea File = New + + project from Existing Sources ... + = Select you unzip the project folder directory and click OK = Select Import Project FR Om External model = Select maven = and click Next until the project is imported successfully.
Method Two: Create using idea Spring INITIALIZR (recommended)

Click File = New + Project + Select Spring Initializr, and follow the bootstrap action, such as:

The final project directory, such as:

Catalogue description

As shown, the entire project directory is very clear, there are several important documents to be said separately:

    • Pom.xml = Maven's build file with reference information about the build
    • The source classes for the Src/main/java = Project are in this directory
    • Com.hellospringboot.hellospringboot/hellospringbootapplication.java = The first half of which is the package name and the second half is the project's startup file
    • Src/main/resources + = Resource file directory for projects
    • Src/main/resources/application.properties = null Property profile
Vi. operation of the project

For demonstration convenience, we add an access method to index directly in Hellospringbootapplication.java, return a hello message, and right-click Run Project.

Attention:

    1. Identify class annotations, before we access, after processing the addition method, we also need to set @restcontroller annotations to class Hellospringbootapplication.java, @ Restcontroller is a collection of @controller and @responsebody, and if only @restcontroller annotations are used, the methods in the controller cannot return to the JSP page, or HTML, the configured view parser Internalresourceviewresolver does not work, the content returned is the contents of return, in order to facilitate the demonstration, we directly to the class annotated @restcontroller annotation;
    2. Identity access method annotations, as long as the method annotations are identified, we can map to the appropriate method, such as we mapped the access address "sayhi", using "Sayhi2" is not access to the

Use advanced

If we need to pass parameters and return JSON-formatted data, we should do this: Repeat the previous step, give the Class A @restcontroller annotation, return the JSON-formatted information, and if there are parameters to add the name of the parameter directly on the method, The parameter name of the method and the URL parameter name must be one by one corresponding to the same, otherwise the obtained parameter value is null:

Summary: The order in which parameters are passed is not related and does not affect the value.

Vii.. Hot Deployment

According to the above tips, we have completed the Spring Boot project creation and operation, but there is a problem is that every time after the code changes need to restart debugging, code to take effect, more trouble, there is a simple way?

The answer is yes, that's the next hot deployment.

The configuration of the hot deployment is as follows:

First step: Set idea to start the project automatically compile

File = Settings = Build,execut,deployment = Compiler Hook in the right Build Project automatically

Such as:

Step two: Enable automatic generation of Project runtime

CTRL + SHIFT + A (Windows system), command+shift+alt+/(Mac System)

Search command: Registry = Check Compiler.automake.allow.when.app.running

Such as:

The final effect is as follows:

Viii. Summary
    • Spring framework from small to large development, and the rise of new technology to make spring development look bloated, people need a simple and efficient method, so spring boot came into being;
    • This article describes the meaning of the spring boot version number and the most appropriate development tool for using idea.
    • This article describes two ways to create a spring boot project, and it is recommended to create a spring initializr with idea;
    • This article describes the meaning of the spring boot directory, as well as the start and run of the project, and describes the use of multi-parameter delivery, as well as the JSON format of the output sample;
    • This article introduces the hot deployment configuration process for spring boot, which facilitates the development and debugging of Spring boot.

Get up, scan, join the circle.

Spring Boot Best Practices (i) Quick start

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.