#Spring Boot learning notes (super detailed) learning records

Spring Boot learning notes (super detailed) learning records (1)

On March 10, 2020, there will be a huge moon in the early morning of this day. It is said that there will be another moon in April

I have written several blogs to share my learning records, and I suddenly want to string my learning process of springboot and all the knowledge involved in it. On the one hand, I can familiarize myself with the knowledge I have learned, on the other hand, I can share it with you. Of course, I also hope that some big people can comment on the problems in my learning process. (at the same time, I hope that I can transform from a rookie when I graduate, and I will share some of my previous projects in the future). No matter if anyone sees it, it is also a mark left by my student days

1, Introduction

1. Introduction to spring boot

  • Simplify Spring application development to a framework;
  • The whole Spring technology stack needs a big integration;
  • One stop solution developed by J2EE

2. micro service

2014,martin fowler

Microservices: architecture style (service miniaturization)

An application should be a group of small services; Interworking can be done through HTTP; single application: ALL IN ONE

Microservices: each functional element is a software unit that can be replaced and upgraded independently;

Refer to the microservice document for details

3. Environmental preparation

Environmental constraints

– jdk1.8: Spring Boot recommends jdk1.7 and above; java version "1.8.0_"

– Maven 3. X: Maven 3.3 and above; Apache Maven 3.3.9

–IntelliJIDEA2017: IntelliJ IDEA 2017.2.2 x64,STS

–SpringBoot 1.5.9.RELEASE: 1.5.10;

1.MAVEN setting

Add the pro les tag to maven's settings.xml configuration file

It is mainly to tell maven to use jdk1.8 later

<profile>
<id>jdk‐1.8</id>
<activation>
<activeByDefault>true</activeByDefault> 5	<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>

2.IDEA settings

Integrate maven;

4,Spring Boot HelloWorld

One function:

The browser sends the hello request, the server accepts the request and processes it, and responds to the Hello World string;

1.) create a maven project; (jar)

2) import spring boot dependency

<?xml version="1.0" encoding="UTF-8"?>
<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>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jdbc</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>


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


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.) write a main program; start the spring boot application

package com.jdbc.demo;

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

@SpringBootApplication
//This is a springboot application @ SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        //Launch spring app
        SpringApplication.run(DemoApplication.class, args);
    }

}

4.) write related Controller and Service

//Can handle request @ Controller
@Controller
public class HelloController {

    //Return the return result to the browser
    @ResponseBody
    //Handle the corresponding request for hello
    @RequestMapping("/hello")
    public String hello(){

        return "hello";
    }
}

5.) run main program test

[failed to transfer the pictures in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-3qLowYXZ-1583823915937)(C:\Users\zy\AppData\Roaming\Typora\typora-user-images\image-20200310141725478.png))

6.) simplified deployment

<!‐‐ This plug-in can package the application into an executable jar Package;‐‐>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐maven‐plugin</artifactId>
</plugin>
</plugins>
</build>

To make this application a jar package, you can directly use the Java jar command to execute it:

OK, no matter what you learn, you have to ask him to say hello to our world. The greeting has already been said. Let's analyze it briefly

5.Hello World exploration

1.) POM file

1.1) parent project
parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
His parent project is
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐dependencies</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath>../../spring‐boot‐dependencies</relativePath>
</parent>
He's in charge Spring Boot All dependent versions in the application;

Version Arbitration Center of Spring Boot;

In the future, we need not write the version by default when importing dependencies. (the dependency that is not managed in dependencies naturally needs to declare the version number.)

1.2) starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>

****spring-boot-starter****-web:

Spring boot starter: spring boot scene starter, which helps us import the components that the web module depends on for normal operation;

Spring Boot extracts all the functional scenarios and makes them into starters one by one. Only these starters need to be introduced into the project

All dependencies of related scenarios are imported. Import the initiator of any scene with whatever function you want

2.) main program class, main program entry class

package com.jdbc.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication To mark a main program class, indicating that this is a Spring Boot application
*/
@SpringBootApplication
//This is a springboot application @ SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        //Launch spring app
        SpringApplication.run(DemoApplication.class, args);
    }

}

@****Springbootapplication * *: the spring boot application annotation indicates that a class is the main configuration class of SpringBoot, SpringBoot

You should run the main method of this class to start the spring boot application;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

@****SpringBootCon fi guidance ***: configuration class of Spring Boot; marked on a class, indicating that it is a configuration class of Spring Boot; @ *** Con fi guidance **: marked on the configuration class;

Configuration class configuration file; configuration class is also a Component in the container; @ Component

@****EnableAutoCon "configuration * *: enable auto configuration function;

What we need to configure in the past, Spring Boot helps us to configure automatically; @ * * * * EnableAutoCon fi configuration * * * tells Spring Boot to enable the automatic configuration function, so that the automatic configuration can take effect;

1@AutoConfigurationPackage
2@Import(EnableAutoConfigurationImportSelector.class)
3public @interface EnableAutoConfiguration {

@****AutoCon "configurationpackage ***: AutoCon" configurationpackages. Register. Class ":

Spring's underlying annotation @ Import imports a component to the container. The imported component is AutoCon "configurationpackages. Register. Class;

Scan all components in the package of the main configuration class (@ class marked by SpringBootApplication) and all subpackages below to the Spring container;

@****Import * * (enableautocon "configurationimportselector. Class); import components to the container?

****EnableAutoCon "configurationimportselector * *: the selector of which components to import;

Return all components that need to be imported in the form of full class names; these components will be added to the container;

Many auto configuration classes (xxxAutoCon "configuration) will be imported into the container, that is, all the components required for this scenario will be imported into the container, and these components will be configured;

With the automatic configuration class, we can write configuration injection function components manually;

SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)

When spring boot starts, get the values specified by EnableAutoCon "configuration from META-INF/spring.factories in the classpath, import these values into the container as auto configuration classes, and the auto configuration classes will take effect to help us with auto configuration; for the things we need to configure, auto configuration classes will help us;

The overall integration solution and automatic configuration of J2EE are in spring boot autocon "gure-1.5.10.RELEASE.jar;

Published 11 original articles, won praise 22, visited 7662
Private letter follow

Tags: Spring Maven SpringBoot Java

Posted on Tue, 10 Mar 2020 03:27:16 -0400 by pbsperry