Getting Started with the SpringBoot Framework

If you want to say what is the most popular enterprise-level development framework in the past two years, it is SpringBoot. This is a very good open source framework. Maybe some friends here will have questions. Now there are so many excellent open source frameworks Why is SpringBoot very popular as soon as it appears? In fact, there is a reason why it is so popular. SpringBoot stood on the shoulders of giants, so who is this giant? It is Spring, an excellent open source framework. Seeing this, everyone should understand. With such an excellent framework as a shoulder, how can SpringBoot not be excellent?

So before officially introducing the SpringBoot framework, let's briefly talk about Spring, an excellent open source framework. For a detailed introduction, please read the Spring article.


1. Spring framework

Spring

The Spring framework is an open source J2EE application framework initiated by Rod Johnson and is a lightweight container for bean lifecycle management. Spring solves many common problems encountered by developers in J2EE development, and provides powerful functions such as IOC, AOP and Web MVC. Spring can be used alone to build applications, and can also be used in combination with many Web frameworks such as Struts, Webwork, and Tapestry, and can be combined with desktop application APs such as Swing. Therefore, Spring can be applied not only to JEE applications, but also to desktop applications and applets.

Features:

1. Convenient decoupling and simplified development

Through the IoC container provided by Spring, we can control the dependencies between objects by Spring to avoid excessive program coupling caused by hard coding. With Spring, users no longer need to write code for low-level requirements such as single-instance mode classes and property file parsing, and can focus more on upper-level applications.

2. AOP programming support

The AOP function provided by Spring facilitates aspect-oriented programming, and many functions that are not easy to implement with traditional OOP can be easily handled through AOP.

3. Support for declarative transactions

In Spring, we can be freed from the monotonous and boring transaction management code, and manage transactions flexibly in a declarative manner, improving development efficiency and quality.

4. Convenient program testing

Almost all testing tasks can be performed in a non-container-dependent programming manner. In Spring, testing is no longer an expensive operation, but something that can be done easily. For example: Spring supports Junit4, and you can easily test Spring programs through annotations.

5. Convenient integration of various excellent frameworks

Spring does not exclude various excellent open source frameworks. On the contrary, Spring can reduce the difficulty of using various frameworks. Spring provides direct support for various excellent frameworks (such as Struts, Mybatis, Hessian, Quartz).

6. Reduce the difficulty of using Java EE API

Spring provides a thin encapsulation layer for many difficult-to-use Java EE APIs (such as JDBC, JavaMail, remote calls, etc.). Through Spring's simple encapsulation, the difficulty of using these Java EE APIs is greatly reduced.

7. Java source code is a classic learning paradigm

Spring's source code is exquisitely designed, clear in structure, and ingenious, reflecting the master's flexible use of Java design patterns and profound attainments in Java technology. The source code of the Spring framework is undoubtedly the best practice example of Java technology. If you want to quickly improve your Java technology level and application development level in a short period of time, learning and researching Spring source code will give you unexpected results.


Two, SpringBoot framework

SpringBoot

SpringBoot is a lightweight new framework developed based on Spring. It not only inherits the original excellent features of the Spring framework, but also further simplifies the entire construction and development process of Spring applications by simplifying configuration. With Spring Boot, it's easy to create self-contained, production-grade Spring-based applications. SpringBoot is also often referred to as a microframework.

Features:

1. Standalone Spring applications can be created, and based on their Maven or Gradle plugins, executable JARs and WARs can be created.

2. Embed Servlet containers such as Tomcat or Jetty.

3. Provides an auto-configured "starter" project object model (POMS) to simplify Maven configuration.

4. Automatically configure the Spring container as much as possible.

5. Provide ready features such as metrics, health checks and externalized configuration.

6. Absolutely no code generation, no XML configuration required.

The most important thing is to reduce a large number of XML configurations. In general, in one sentence, using the SpringBoot framework to develop projects can easily create independent, production-level Spring-based applications


3. SpringBoot environment construction

I believe that everyone has a basic understanding of SpringBoot. I have been saying how excellent SpringBoot is, but you have not actually built a SpringBoot environment. It is difficult for you to experience the simple and fast development of SpringBoot. Let me explain Let's simply build a SpringBoot development environment, so that everyone can experience how efficient SpringBoot is.

SpringBoot small case directory structure

The first step is to create a new maven project (create a new SpringBoot project in the form of maven), select the skeleton, click webapps, click next, and click all the way as needed.

The second step is to introduce dependencies, and pom.xmlintroduce SpringBoot parent project dependencies and web dependencies in the file.

<!--继承springboot的父项目-->
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.4.RELEASE</version>
</parent>
<!--引入web支持-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

The third step is src/main/resourcesto create a new configuration file in the directory application.yml(only in this directory), and specify the SpringBoot project name (optional, or not specified).

server:
  servlet:
    context-path: /springboot

The fourth step is to develop SpringBoot's global entry class, which is located above all subpackages.

package cn.ppdxzz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description: SpringBoot入口类
 * @Author: PeiChen JavaAnything
 */
@SpringBootApplication
@RestController
public class Application {
    
    
    public static void main(String[] args) {
    
    
        //启动SpringBoot应用,参数一:入口类类对象,参数二:main函数参数
        SpringApplication.run(Application.class,args);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "SpringBoot") String name) {
    
    
        return String.format("Hello %s!", name);
    }
}

The fifth step is to start the SpringBoot project, enter http://localhost:8080/springboot/hello in the browser, and you will see the output informationhello SpringBoot

Finally, I would like to give you a fun thing, that is, SpringBoot supports custom banners, SpringBoot banners, which have been broken by programmers, hahaha. The following is my banner, you can find it online directly, there are many, and it also supports online generation, and it is very easy to use, just create a new one src/main/resourcesunder banner.txtcopy the content into it.



So far, a simple SpringBoot application environment has been built. Some people may say that it is necessary to write configuration files. What I want to say here is that these are the most basic configuration files of SpringBoot. If you still don’t think that SpringBoot is worthy of the name of rapid development framework, you can read another article of mine: SSM configuration file. After you read that article, you will find that, wow, SpringBoot has very few configuration files. .

In fact, the reason why you save so much trouble and don’t have to write so many configuration files before is mainly due to the fact that SpringBoot has already automatically configured it for you at the bottom layer, and you only need to take it and use it. This is the end of this SpringBoot quick start. Next time when you have nothing to do, I will discuss with you what SpringBoot does at the bottom.

Guess you like

Origin blog.csdn.net/active_pig/article/details/107634725