SpringBoot study notes (1): Introduction to SpringBoot and entry cases

1. Introduction to SpringBoot

1. Introduction to SpringBoot

Spring Boot is a new framework provided by the Pivotal team. Its design purpose is to simplify the initial setup and development process of new Spring applications. The framework uses a specific method for configuration and minimizes configuration files, so that developers no longer need to define boilerplate configurations. In this way, Spring Boot is committed to becoming a leader in the booming field of rapid application development.

Four main features of SpringBoot:

  • SpringBoot Starter: He grouped commonly used dependencies for integration and merged them into one dependency, so that they can be added to the Maven or Gradle build of the project at one time;
  • Automatic configuration: SpringBoot's automatic configuration feature takes advantage of Spring4's support for conditional configuration, reasonably speculates the beans required by the application and automatically configures them;
  • Command-line-interface: (Command-line-interface, CLI): SpringBoot's CLI takes advantage of the Groovy programming language and combines automatic configuration to further simplify the development of Spring applications;
  • Actuatir: It builds a small application for all the features of SpringBoot applications. But first, we quickly understand each feature and better experience how they simplify the Spring programming model.

2. The core functions of SpringBoot 

(1) Independent running Spring project 

SpringBoot can be run independently in the form of jar package, and running a SpringBoot project only needs to pass java -jar xx.jar. 

(2) Built-in Servlet container 

Spring Boot can choose to embed Tomcat, Jetty or Undertow, so that it does not need to be deployed in the form of a war package. 

(3) Provide starter to simplify maven configuration 

Spring provides a series of starter poms to simplify the loading of maven dependencies. For example, when springboot-starter-web is used, related dependencies are automatically added, without the need to manually add each coordinate dependency. 

(4) Automatic configuration of Spring 

Spring Boot will automatically configure Beans for the classes in the jar package according to the jar packages and classes in the classpath, which will greatly reduce the configuration we need to use. Of course, SpringBoot only considers most development scenarios, not all scenarios. If in actual development, we need to automatically configure the bean, but Spring Boot does not provide support, you can customize the automatic configuration. 

(5) No code generation and xml configuration 

The magic of SpringBoot is not achieved by code generation, but by conditional annotations. This is a new feature provided by Spring 4.x. Spring 4.x advocates the use of java configuration and annotation configuration combined, while SpringBoot does not All configurations of SpingBoot can be realized by any xml configuration.

3. Features of SpringBoot

  • Provide a very fast and widely accepted introductory experience for all Spring developers
  • It works out of the box (starter is actually a jar package provided by SpringBoot), but you can quickly get rid of this method by setting the parameters (.properties) by yourself.
  • Provides some non-functional features common in large-scale projects, such as embedded server, security, indicators, health detection, external configuration, etc.
  • SpringBoot is not an enhancement of Spring functionality, but provides a quick way to use Spring

4. Advantages and disadvantages of SpringBoot

(1) Advantages:

  • Quickly build projects: Omit tedious and repetitive xml configuration, build a web project in minutes.
  • Configuration-free integration of mainstream development frameworks: A lot of Starter dependency packages are provided, which can be used out of the box without extra configuration.
  • The project can run independently: no external dependency on the Servlet container.
  • Greatly provide development and deployment efficiency.
  • Simple monitoring: The actuator package is provided, and you can use it to monitor your application. 

(2) Disadvantages:

  • Too many dependencies: A simple SpringBoot application has dozens of M only.
  • Lack of monitoring integration solutions and safety management solutions: only basic monitoring is provided. To achieve production-level monitoring, the monitoring solution needs to be solved by yourself

Second, the entry case

1. SpringBoot+maven builds helloworld.

(1) Create the maven project SpringBootHelloWorld.

(2) Introduce dependent packages in pom.xml.

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

(3) Create HelloController in the controller.

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello SpringBoot";
    }
}

(4) Create HelloApplication under com.yht.

@SpringBootApplication
public class HelloApplication {
    public static void main( String[] args ) {
        SpringApplication.run(HelloApplication.class);
    }
}

(5) Start the program and enter: http://localhost:8080/hello in the address bar of the browser , the page return result is as follows:

Extension: Type the SpringBoot project into a jar package.

a. Add the configuration in pom.xml as follows:

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

 b. Click package.

A .jar package will be generated in c.target, as follows:

d. Find the path of the corresponding jar package and run it successfully through the java -jar name .

2. Spring Initializr creates the SpringBoot project.

(1) Select Spring Initializr, specify the JDK version, and click next.

(2) Fill in the parameters in the figure below and click next.

Parameter Description: 

parameter Description
Group Generally reverse domain name format
Artifact Unique identifier, generally the project name specific maven related information, you can search by yourself
Type Project type here select maven project
Language Programming language
Packaging Packing type  
Java Version jdk version 
Version Project version number 
Name project name
Description project description 
Package

Package name, generally default

(3) Select the dependent package and the version of SpringBoot, and click next. 

(4) Fill in the project name, then select the local storage path of the project, and click Finish.

(5) The project is successfully created and HelloController is created.

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello SpringBoot Quick";
    }
}

The project directory structure is as follows:

Description of the file directory under resources:

  • static: save all static resources, including: js, image, css.
  • templates: Save all template pages, such as freemarker, thymeleaf.
  • application.properties: SpringBoot configuration file. The default port number of springboot is 8080. If you modify the default port number and context path, you only need to set it in application.properties or application.yml, which will be explained in detail later. 

(6) Start the project and enter: http://localhost:8080/hello in the address bar of the browser . The data returned by the page is as follows:

 

 

Guess you like

Origin blog.csdn.net/weixin_47382783/article/details/114013752