The core mechanisms of Spring Boot mainly include automatic configuration, starting dependencies, main classes and runners, and embedded servers. Below we will introduce these core mechanisms in detail to get a preliminary understanding of Spring Boot.
1. Auto-configuration
Automatic configuration is one of the most important features of Spring Boot, which automatically configures Spring applications based on dependencies in the classpath. Spring Boot through a series of@Conditional
Annotation to determine which configuration classes should be loaded.
1.1 @SpringBootApplication Annotation
@SpringBootApplication
It is a combination annotation that contains the following annotations:
-
@Configuration
: Mark this class as a configuration class. -
@EnableAutoConfiguration
: Enable automatic configuration. -
@ComponentScan
: Enable component scanning, automatically discover and register Spring components.
@SpringBootApplication public class MyApplication { public static void main(String[] args) { (, args); } }
1.2 @EnableAutoConfiguration annotation
@EnableAutoConfiguration
Annotations trigger the automatic configuration mechanism. Spring Boot will look for the classpath with@Configuration
Annotated classes and decide whether to apply these configurations based on conditions (such as dependencies in the classpath).
1.3 Conditional Annotation (@Conditional)
Spring Boot uses various@Conditional
Annotation to control the loading conditions of the configuration class. Common conditional annotations include:
-
@ConditionalOnClass
: Take effect when a specified class exists in the classpath. -
@ConditionalOnMissingBean
: Effective when there is no bean in the container. -
@ConditionalOnProperty
: Take effect when the specified attribute exists in the configuration file.
2. Starter Dependencies
Starting dependencies are a set of dependencies management provided by Spring Boot, designed to simplify project configuration. Each startup dependency contains a set of related dependencies, allowing developers to introduce multiple related libraries by just declaring one dependency.
2.1 Common Starting Dependencies
-
spring-boot-starter-web
: Contains the dependencies required to build web applications, such as Spring MVC and embedded Tomcat. -
spring-boot-starter-data-jpa
: Contains the dependencies required to build the data access layer, such as Spring Data JPA and Hibernate. -
spring-boot-starter-test
: Contains the dependencies required for testing, such as JUnit, Mockito, etc.
2.2 Maven dependency example
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
3. Main Class and Runners
Spring Boot applications usually have a main class that containsmain
Method, used to start the application.
3.1 Main Class
Main class is usually used@SpringBootApplication
Annotation and includemain
method.
@SpringBootApplication public class MyApplication { public static void main(String[] args) { (, args); } }
3.2 SpringApplication Class
SpringApplication
The class is responsible for launching the Spring Boot application. It performs the following tasks:
Initialize the Spring application context. Load the configuration file. Perform automatic configuration. Start the embedded server (if applicable). 3.3 Runner (ApplicationRunner
andCommandLineRunner
)
You can achieveApplicationRunner
orCommandLineRunner
Interface, performs some initialization operations after the application is started.
@Component public class MyRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { ("Application started!"); } }
4. Embedded Server
Spring Boot supports a variety of embedded web servers, such as Tomcat, Jetty, and Undertow. By default, Spring Boot uses Tomcat as the embedded server.
4.1 Embedded Tomcat
Spring Boot Byspring-boot-starter-web
Dependencies automatically include Tomcat's related dependencies. You can customize the server's port, context path and other properties through configuration files.
4.2 Custom server
If you want to use another server, you can do so by excluding the default Tomcat dependencies and adding dependencies from other servers.
Maven example
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId></groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
5. Configuration Files
Spring Boot supports multiple configuration file formats, such asand
. These files are used to configure various properties of the application.
5.1 Example
=8080 =jdbc:mysql://localhost:3306/mydb =root =root
5.2 Example
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: root password: root
6. Summary
The core mechanisms of Spring Boot include automatic configuration, start-up dependencies, main classes and runners, and embedded servers. These mechanisms work together to make Spring Boot application development easier and more efficient. Through automatic configuration, Spring Boot can automatically configure applications based on dependencies in the classpath; through initial dependencies, developers can easily manage project dependencies; through main classes and runners, applications can easily start and perform initialization operations; through embedded servers, applications can run as an independent JAR file.
That's it. Later, the author will introduce how to customize the SpringApplication to do some customized actions.
This is all about this article about the core mechanism of Springboot. For more related content of Springboot core mechanism, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!