SpringBoot (a) - Getting Started

A, Spring Boot entry

1, Spring Boot Profile

  • Spring a simplified application development framework;
  • Spring a large integration of the entire technology stack;
  • J2EE development of one-stop solution;

2, micro-services

  • 2014 - martin fowler proposed
  • Micro services: architectural style (micro-based service)
  • An application should be a set of small service; can communicate via HTTP way;
  • Monomers Used: ALL IN ONE
  • Micro-services: Each functional element is an independent final alternative upgrade and software independent unit;

1, MAVEN set

To the profiles of the label maven settings.xml configuration file is added (that is, the default setting maven support jdk version)

<profile>
  <id>jdk-11</id>
  <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>11</jdk>
  </activation>
  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
  </properties>
</profile>

2, create a maven project; (jar)

3, spring boot introduced related dependencies

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

4, write a main program; launch applications Spring Boot

/**
 * @Author: Mr_OO
 * @Date: 2020/1/10 15:08
 * 标明这是一个主程序类
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

5, related to the preparation Controller, Service

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

6, run the main program test

7, simplify deployment

 <!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework .boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Application of this labeled jar package directly java -jar command execution;


Hello World Exploration

1, POM file

1, the parent project

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

他的父项目是
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
他来真正管理Spring Boot应用里面的所有依赖版本;

Spring Boot version Arbitration Center;

After we import dependence is no need to write the default version; (there is no need to declare a natural version number which rely dependencies management)

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 initiator; help us introducing the assembly modules running web depends;

SpringBoot all the scenes are extracted features, one made of Starters (initiator), are dependent only need to introduce all of these scenarios associated starter introduced in coming projects inside. What is the function to use it to import the starter what scene.

2, of the main program, the main entrance class

/**
 *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

@ SpringBootApplication : the Spring marked the Boot application instructions on a class of this class is the main class configuration of SpringBoot, SpringBoot should run the main method of this class to start SpringBoot 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 {

@ SpringBootConfiguration : the Spring of the Boot configuration class;

Marked on a class, he said it was a Spring Boot configuration class;

@ The Configuration : configuration class marked up this comment;

----- profile configuration class; configuration class is a container assembly; @Component

@ EnableAutoConfiguration : turn on auto-configuration feature;
things before we need to configure, Spring Boot to help us to automatically configure; @ EnableAutoConfiguration tell SpringBoot open the automatic configuration capabilities; the automatic configuration to take effect;

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

@ AutoConfigurationPackage : Automatic configuration package

​ @Import(AutoConfigurationPackages.Registrar.class):

Spring bottom notes @Import, the vessel is introduced into a component; component introduced by AutoConfigurationPackages.Registrar.class;

Where all components of all primary package configuration class (@SpringBootApplication class label) and the lower sub-packets of scanned Spring container inside;

​ @Import(EnableAutoConfigurationImportSelector.class);

Import component to the container

EnableAutoConfigurationImportSelector : which introduced the selector assembly;

All of the components needed to return the introduced full class name manner; these components will be added to the container;

It will give a lot of container imported automatic configuration class (xxxAutoConfiguration); this scenario is to import all the components required for container and configure these components;

With automatic configuration class, eliminating the need to manually write the configuration we inject functional components such work;
SpringFactoriesLoader.loadFactoryNames (EnableAutoConfiguration.class, classLoader);
Spring Boot EnableAutoConfiguration Gets the value specified by the META-INF / spring.factories class path at startup, these values ​​automatically configured as class into the container, automatically configured based on force, work for us automatically configured; What we need before their own configuration, automatic configuration classes are configured to help us better;

J2EE overall integration solutions and automatic configuration are spring-boot-autoconfigure-2.2.1.RELEASE.jar in;


Spring Boot quickly create a project using the Spring Initializer

1, IDEA: quickly create a project using Spring Initializer

IDE support the creation wizard to quickly create a project using Spring Spring Boo t projects;

Select the module we need; networking wizard to create a Spring Boot project;

The default generated Spring Boot project;

  • The main program has generated good, we just need our own logic
  • resources folder in the directory structure
    • static: Save all static resources; js css images and so on;
    • templates: Save all template pages; (Spring Boot default jar package using an embedded Tomcat, the default does not support JSP page); you can use a template engine (freemarker, thymeleaf);
    • application.properties:Spring Boot application configuration file; you can modify some of the default settings;

2, STS using Spring Starter Project quickly create a project (eclipse)

Published 58 original articles · won praise 7 · views 9235

Guess you like

Origin blog.csdn.net/Mr_OO/article/details/103923876