SoFunction
Updated on 2025-05-20

Detailed explanation of the basic framework of SpringBoot

SpringBoot Basics – Introduction to the Framework

introduce

1.1 Overview

The purpose of SpringBoot development is to simplify the creation, operation, debugging and deployment of Spring applications. Using Spring Boot can focus on the development of Spring applications without paying too much attention to XML configuration. Simply put, it provides a bunch of dependency packaging and has solved the dependency problem as per usage habits. Using Spring Boot can be used without or requires very little Spring configuration to enable enterprise projects to run quickly.

1.2 Core functions

  • Run Spring Projects independently: SpringBoot can be run as a standalone JAR package.
  • Embed Servlet: SpringBoot can choose to use Tomcat, Jetty or Undertow, without deploying projects in the form of war packages.
  • Simplified configuration: Spring provides recommended basic POM files to simplify Maven configuration.
  • Automatically configure Spring: SpringBoot will automatically configure the Spring framework based on project dependencies, greatly reducing the configuration to be used by the project.
  • Production-ready features are provided: Provides features that can be used directly in a production environment, such as performance metrics, application information, and application health checks.
  • Codeless generation and XML configuration: SpringBoot does not generate code. All configurations of Spring are not required at all.

2. Framework Differences

2.1 SpringBoot and Spring

SpringBoot Web components integrate SpringMVC framework by default. After SpringMVC3.0, it supports annotation method to use java code to start SpringMVC.

2.2 SpringBoot and SpringCloud

  • SpringBoot quickly integrates third-party frameworks (Maven dependency###Maven inheritance), completely adopts annotation, simplifies XML configuration, and ultimately executes it in a Java application.
  • SpringCloud has a complete microservice solution framework with very powerful functions, such as registration center, client calling tools, service governance (load balancing, circuit breakers, distributed configuration center, network management, message bus, etc.).
  • Relationship: Microservice communication technology Http+json (restfull) is lightweight. SpringBoot Web components integrate SpringMVC by default. SpringCloud relies on SpringBoot to implement microservices and uses SpringMVC to write microservice interfaces.

3. Detailed explanation of the framework

3.1 Simplify project creation

During Spring, users need to manually add project dependencies in the pom, and the starter-web component in SpringBoot already contains multiple dependencies, which can greatly simplify the project creation process.

<!-- .....Omit other dependencies -->
<dependency>
    <groupId></groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.0.</version>
    <scope>compile</scope>
</dependency>

3.2 Simplify configuration

SpringBoot uses Java Config to configure Spring. The following demonstrates how to get SpringBoot to manage a class and method

public class TestService {
    public String sayHello () {
        return "Hello Spring Boot!";
    }
}
import ;
import ;
@Configuration
public class JavaConfig {
    @Bean
    public TestService getTestService() {
        return new TestService();
    }
}

@Configuration means that the class is a configuration class, and @Bean means that the method returns a Bean. In this way, TestService is used as a bean and let Spring manage it. In other places, if we need to use the bean, just like before, we can use the @Resource annotation and use it, which is very convenient.

3.3 Simplify deployment

When using Spring, when deploying the project, we need to deploy tomcat on the server, and then we will type the project into war package and throw it into tomcat. After using SpringBoot, we do not need to deploy tomcat on the server, because SpringBoot has tomcat embedded in it. We only need to type the project into jar package and start the project with one-click using java -jar.

Common annotations

4.1 @SpringBootApplication

The core annotation of SpringBoot, used on the SpringBoot main class, is used to start SpringBoot various components

4.2 @EnableAutoConfiguration

SpringBoot automatically configures annotations. After using this annotation, SpringBoot can configure SpringBeans according to the package or class under the current classpath.@EnableAutoConfigurationThe key to implementation is to introduce AutoConfigurationImportSelector, with the core logic as the selectImports method

  • Load all possible automatic configuration classes from the configuration file META-INF/.
  • Deduplication and exclude classes carried by exclude and excludeName properties.
  • Filter, return the automatic configuration class that meets the condition (@Conditional)

4.3 @Configuration

Define the configuration class and indicate that the class is the source of Bean configuration information and is generally added to the main class. If some third-party libraries need to be usedxml file, it is recommended to still use the @Configuration class as the main configuration class of the project - the xml configuration file can be loaded using the @ImportResource annotation.

4.4 @ComponentScan

Component scan. Have springBoot scan to the Configuration class and add it to the program context. The @ComponentScan annotation will assemble the classes identified by @Controller, @Service, @Repository, and @Component annotations into the spring container.

4.5 @Repository

Label the data access component and DAO component. Using the @Repository annotation can ensure that DAO or repositories provides exception translation. The DAO or repositories class modified by this annotation will be discovered and configured by ComponetScan, and there is no need to provide them with XML configuration items.

4.6 @Service

Components generally used to modify service layer

4.7 @RestController

It is used to annotate the control layer components, represented as controller beans, and write the return value of the function directly into the HTTP response body. It is a REST style controller. It is a collection of @Controller and @ResponseBody.

4.8 @RequestBody

Indicates that the return value of this method is written directly into the HTTP response body. It is generally used when obtaining data asynchronously. After using @RequestMapping, the return value is usually parsed as a jump path. After adding @responsebody, the result returned will not be parsed as a jump path, but will be directly written into the HTTP response body. For example, if you get json data asynchronously and add @responsebody, the json data will be returned directly.

4.9 @Component

It refers to components in general. When components are not easy to classify, we can use this annotation to mark them.

4.10 @Bean

It is equivalent to XML, placed on top of a method, rather than a class, which means to generate a bean and hand it over to spring management.

4.11 @Autowired

byType method. Use the configured beans to complete the assembly of properties and methods. It can mark class member variables, methods and constructors to complete the automatic assembly work. When (required=false) is added, an error will not be reported even if the bean cannot be found.

4.12 @RequestMapping

RequestMapping is an annotation for processing request address mapping. Provides routing information, responsible for the URL to the specific function mapping in the Controller, and can be used on classes or methods. Annotation The methods that represent all response requests in a class on a class are taken as the parent path.

4.13 @RequestParam

Before the method parameters. example:
@RequestParam String a =(“a”)。

4.14 @PathVariable

Path variable. The parameters must be the same as the names in the braces.

RequestMapping("user/get/mac/{macAddress}")
public String getByMacAddress(@PathVariable String macAddress){
//do something;
}

This is the end of this article about the detailed explanation of SpringBoot basic framework. For more related SpringBoot basic framework content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!