(1) Initial SpringBoot

Interested friends can go to know about the other posts have been published ha, your like is the biggest support for me, thank you!

  • The article directories

SpringBoot Takes off — HelloWorld

(2) SpringBoot take-off road – entry principle analysis

(3) SpringBoot Take-off Road -YAML Configuration summary (Get Started must know must know)

(4) SpringBoot Take-off Road – Static resource processing

(5) SpringBoot Take-off Road -Thymeleaf template engine

(6) SpringBoot Take-off Road – integration of JDBCtemplate-Druid-Mybatis

(7) SpringBoot Take-off Road – Integrating SpringSecurity (Mybatis, JDBC, memory)

(8) SpringBoot Take-off Road – Integrated Shiro detailed tutorial (MyBatis, Thymeleaf)

(9) SpringBoot Take-off Road -Swagger 2 And 3

Description:

  • SpringBoot take off road series of articles involving the source code, are synchronized to Github, there is a need for small partners, free to down
    • Github.com/ideal-20/Sp…
  • Talent and learning shallow, will point shallow knowledge, everyone right as a tool to see it, do not like spray ha ~

(1) The road to learning

Develop aWeb project, from the introduction of JavaWeb, which is Servlet + Tomcat set, after learning this part, most people will contact some frameworks, such as SSM, SSH (less), and then to contact SpringBoot, and so on, with the emergence of new frameworks, Or a unified framework of the version of the big update, we also need to continue to learn, in fact, like I usually do a lot of things, namely simple or complex additions, modifications and checks, according to the needs of mixed with a lot of business logic, for example, a simple login everyone in learning should have done N versions of it

Of course, there are also some friends who skip Spring and watch SpringBoot first, and then go back to learn, but I am not used to it. SpringBoot has done so many things for you. It is easy to go from thrift to luxury, but it is difficult to go from luxury to thrift.

Description:

  • If you don’t want to see the red tape just yet, the actual implementation of SpringBoot project creation can be found under the heading “IDEA to quickly create a SpringBoot project”

  • Originally, I wanted to write some commonly used introductory knowledge into an article and then send it. Later, I felt that the mobile terminal length was too long and the reading experience was too poor, so I planned to divide it into several articles

  • Of course, for each big guy, this is not what, right when a tool to see, do not like not angry ha ~

(2) The development of the framework

To this point, the development of Java enterprise application can be briefly divided into the following three processes: Javaweb –> Spring –> SpringBoot

A: Why frames?

As I said in my previous article, these frameworks can greatly reduce the cost of development, but can effectively implement some requirements. To give you a brief, I have written some superficial view:

You’ve all heard the phrase “a framework is a work in progress”, but a framework encapsulates a lot of code, and then you just call the API that it gives you, and then you can use what it does, right

Framework, of course, the original intention is to simplify the development, of course, helping developers to quickly complete the project demand, says the precise point, is the framework of the combination of a lot of design pattern, can let you “dynamic” development, the code implements the generality and generally write simple code, involve too many “hardcoded” problem. It’s too complicated to write the design patterns yourself

B:为什么用 SpringBoot

Spring helps people build an enterprise application relatively efficiently and quickly, but the Configuration is relatively tedious. After version 2.5, annotation-based component scanning was introduced, which has reduced the AMOUNT of XML. After version 3.0, you can use @Configuration to define Configuration classes. XML could be eliminated altogether, or called annotation-only development, but most people are still used to XML + annotation development

  • Spring has eliminated the XML configuration to some extent, but these configurations are a bit cumbersome to be honest, and inevitably take some time to configure

  • Moreover, the pit of dependence, really a snot a tear, different versions of dependence on crazy conflict is not compatible, always let me crazy

SpringBoot, a framework developed in this context, is a great solution to our problems and makes development easier. Let’s take a look at it

(2) SpringBoot smells good

A: How to solve the problems of the past

SpringBoot, based on past problems with Spring and others, has come up with the idea of convention over configuration. It has a lot of Settings by default, and it does a lot of things behind the back that allow developers to quickly build projects with a small amount of configuration, or integrate some third party stuff. Instead of spending too much time on configuration, developers can focus on writing the business logic, which greatly improves the efficiency of development

B: Basic concepts

SpringBoot does not need to be understood as a new framework. It is not designed to replace Spring. It is designed to simplify Development of Spring

SpringBoot official note:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration

C:

  • Provides a faster start up experience for Spring-based development
  • Straight up, no redundant code generation, no XML configuration
  • Embedded containers greatly simplify Web projects
  • Designed to simplify Spring, not replace Spring

SpringBoot official note:

Features

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated ‘starter’ dependencies to simplify your build configuration
  • Automatically configure Spring and 3rd party libraries whenever possible
  • Provide production-ready features such as metrics, health checks, and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

D: Core functions

Start relying on

  • Starting dependencies are essentially a Maven Project Object Model (POM) that defines transitional dependencies to other libraries, which together support a feature
  • Simply put, a starting dependency is a collection of coordinates that have a certain functionality and provide some default functionality. You just tell Spring Boot what functionality is needed and it will introduce the required library

Automatic configuration

  • Spring Boot automatically provides configuration for many application features common to Spring applications
  • At the same time, Spring Boot auto-configuration is a run-time (more precisely, application startup) process that takes into account a number of factors before deciding which Spring configuration should and should not be used

After a simple study, and then come back to look at these words, you know how strong SpringBoot, but we can not be giants, as we stand on the shoulders of giants to learn! Take off!!

2. Hello SpringBoot

(1) First experience (Maven creation)

Note in advance: this method, after the basic will not use, just for the second point and the third point to pave the way, so as to better understand, SpringBoot is simple, automatic

To create an empty Project, we will create the mudule in the following ways, and create the words directly in Project as well

A: Create Maven Module

To create a Module, select the Maven project, uncheck the web skeleton we used to use, and just create it

Fill in these basic values

GroupID is the unique identifier of the project organization and can generally be set in accordance with the package structure, that is, the Java directory structure in the main directory. It can be set in reverse order of the domain name, but this is not mandatory. For example, I set it to cn.ideal

ArtifactID is the unique identifier for the project and is usually set to the name of the project

It is through these two values that a “coordinate” is formed to ensure the uniqueness of the item

There’s nothing to say about this step. Everybody’s familiar with it

B: Add a starting dependency

According to The requirements of SpringBoot, we need to add its starting dependencies for simple testing

  • The starting point for a project to inherit SpringBoot depends on the Spring-boot-starter parent
  • In order to integrate SpringMVC for the development of Controller, the project needs to import the Startup of web to rely on Spring-boot-starter – Web

      
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7. RELEASE</version>
    </parent>

    <groupId>cn.ideal</groupId>
    <artifactId>springboot_01_start</artifactId>
    <version>1.0 the SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
Copy the code

C: Write the SpringBoot boot class

Our package structure is now cn.ideal and we will create a class in this directory called MySpringBootApplication (name is not mandatory)

Write the following, don’t forget the comments, just know for now that this startup class is critical and will be explained in a few articles

package cn.ideal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringBootApplication {
    public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class); }}Copy the code

D: Create a controller

Then it’s familiar, I created the Controller package under cn. Ideal, and then I created one

package cn.ideal.controller;

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

@Controller
public class QuickStartController {
    @RequestMapping("/test")
    @ResponseBody
    public String test(a){
        return "Springboot access test, take off, fly fly ~ ~ ~"; }}Copy the code

E: Testing SpringBoot

Open the MySpringBootApplication startup class you just created, run its main function, and the console will print out such a pattern

Tomcat started on port(s): 8080 (HTTP) with context path “” this means we can access it through port 8080, and we have not set the application name, so we can access it directly from the port

. ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ () |' _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.7. RELEASE) the 2020-05-10 22:11:34. 973 INFO 30580 - [the main] cn. Ideal. MySpringBootApplication: Starting MySpringBootApplication on LAPTOP-5T03DV1G with PID 30580 (F:\develop\IdeaProjects\framework-code\springboot_01_demo\springboot_01_start\target\classes started by abc in F:\develop\IdeaProjects\framework-code\springboot_01_demo) 2020-05-10 22:11:34.976 INFO 30580 -- [main] cn.ideal.MySpringBootApplication : No active profile set, falling back to default profiles: The default 22:11:35 2020-05-10. 30580-686 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat initialized with port(s): 8080 (HTTP) 22:11:35 2020-05-10. 30580-693 the INFO [main] o.a pache, catalina. Core. StandardService: Starting the service [Tomcat] 2020-05-10 22:11:35. 693 INFO 30580 - [the main] org. Apache. Catalina. Core. StandardEngine: Starting Servlet engine: [Apache Tomcat/9.0.34] 2020-05-10 22:11:35.765 INFO 30580 -- [main] O.A.C.C.C. [Tomcat].[/] : Initializing Spring Embedded WebApplicationContext 2020-05-10 22:11:35.766 INFO 30580 -- [main] o.s.web.context.ContextLoader : Root WebApplicationContext: Initialization completed in 747 ms 2020-05-10 22:11:35.884 INFO 30580 -- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-05-10 22:11:35.990 INFO 30580 -- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (HTTP) with the context path '22:11:35 2020-05-10. 30580-993 the INFO [main] cn. Ideal. MySpringBootApplication: Started MySpringBootApplication in 1.318 seconds (JVM running for 2.665)Copy the code

Direct access to the home page is a white page, because we did not set the index page, so it will be

We asked the controller, and our string was successfully printed

(2) Create project on the Web page of the official website

This approach also lays the groundwork for the third point, using the Spring Initializr Web page to create the project,

1. Open the Spring Initializr page start.spring. IO/on the official website

2, fill in the project information, we will talk about the third point

3. Click the “Generate Project” button to Generate the Project and download it

4. After decompression, import it with IDEA as Maven project

(3) Quick creation of SpringBoot project with IDEA (※)

This is the way we usually create it

A: Create the Spring Initializr Module

Look at the URL is not immediately clear, he default is to go to the official website this site generation, but do not need to import and so on, directly can be generated in the IDEA

B: Fill in the project information

This section, first filled in Group and Artifact, and then the default package name with our project name, which we removed to leave a basic package structure

We use the default version, of course, according to their own choice to change

C: Select the initialization group

In this step, you can choose which components to load for us when we initialize them. For starters, you can choose Web. I’ve also selected DevTools, which we’ll use later in the hot deployment

Continue to determine the location and name

At this point, our project is created

D: What does the project generate

The above steps complete the creation of the base project. The following files are automatically generated

  • Program main start class, he is automatically named according to the project name, such as Springboot02QuickstartApplication

  • A test class, Springboot02QuickstartApplicationTests

  • An application.properties configuration file

  • A POM.xml, which also helps us automatically import some dependencies based on the initialization component we selected earlier

As you can see, we started the class and the POM without having to create it ourselves, which saved us a lot of effort

The autogenerated POM.xml is shown below, with a few comments


      
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>cn.ideal</groupId>
    <artifactId>springboot_02_quickstart</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>springboot_02_quickstart</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <! -- Web related -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <! -- Related to hot deployment -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <! -- Related to SpringBoot unit tests -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <! -- Remove dependencies -->
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <! -- Package plug-in -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Copy the code

(3) SpringBoot hot deployment

I continue to recommend two more common things, the first is hot deployment

In development, we often change classes, pages, and so on. After each change, we need to restart, which is cumbersome and time-consuming. We can add the following configuration in pom.xml to make the code take effect without restarting, which we call hot deployment

(1) Add dependencies

Here are the dependencies for hot deployment. If we choose DevTools when initializing the component, we don’t need to add the dependencies ourselves

<! -- Hot Deployment configuration -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>
Copy the code

The configuration does not work, because IDEA is not automatically compiled by default, so we need to modify some Settings

(2) Modify the configuration

In Settings, find compile-related and select Build Project Automatically

Next, combine Shift+Ctrl+Alt+/ and select Registry

Found in the following, complier. Automake. Allow. The when. App. Running and then checked, then CLOSE out

Restart the server to see the effect, and when we make changes to classes and so on, the code can take effect directly

(IV) The project is packed into Jar packages

We have such a JAR package above, how to use it?

<plugin>
	<! -- Package plug-in -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Copy the code

On the right we can select Clean and then package. When the console displays BUILD SUCCESS, there will be an extra JAR package under target. For example, my springboot_02_quickstart-0.0.1 -snapshot.jar

When successful, we can point to target to see the effect. When successful, the browser can also access it

At the end

If there is any shortage in the article, welcome to leave a message and exchange, thank you for your support!

If it can help you, then pay attention to me! If you prefer the way of reading wechat articles, you can follow my official account

Here we do not know each other, but in order to their dreams and efforts

A adhere to push the original development of technical articles of the public number: the ideal of more than twenty