Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle of starter in Spring Boot and how to configure it automatically

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about the principle of starter in Spring Boot and how to configure it automatically. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Starter background

Spring Boot has become one of the necessary skills of back-end development, and one of the main reasons is that there is a very important mechanism (starter mechanism) in Spring Boot.

Starter can abandon the previous complicated configuration and integrate it into starter. When you use it, you only need to introduce the corresponding starter dependency into maven, and Spring Boot can automatically scan the information to be loaded and start the corresponding default configuration.

Starter frees us from the handling of various dependent libraries and various configuration information. SpringBoot automatically discovers the required Bean through the class under the classpath path and registers it into the IOC container. Spring Boot provides spring-boot-starter dependency modules for daily enterprise application research and development scenarios. All of these dependency modules follow the conventional default configuration and allow us to adjust these configurations, that is, to follow the concept of "convention is greater than configuration".

We often see or use all kinds of xxx-starter. For example, the following:

Spring Boot starter principle

On the whole, it is nothing more than introducing the Jar package into the project as a dependency of the project. Now it's more difficult because we introduced Spring Boot Starter, so we need to understand how Spring Boot loads Spring Boot Starter's Jar package. Let me say a few words.

SpringBoot looks for the / META-INF/spring.factories file in the dependent starter package at startup, and then scans the Jar package on which the project depends according to the Jar package configured in the file, which is similar to Java's SPI mechanism.

For details, you can use the @ Conditional series annotations to configure more precise conditions for loading Bean.

JavaSPI is actually a dynamic loading mechanism implemented by the combination of "interface-based programming + policy mode + configuration file".

Customize the conditions of starter

If you want to customize Starter, you first need to implement automatic configuration, and to achieve automatic configuration, you need to meet the following two conditions:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Can automatically configure the configuration information required by the project, that is, automatically load the dependent environment

The Bean can be automatically generated according to the information provided by the project and registered in the Bean management container

Implement a custom starter

Org.springframework.boot spring-boot-autoconfigure 2.0.0.RELEASE org.springframework.boot spring-boot-configuration-processor 2.0.0.RELEASE true

The implementation process of customizing Starter as needed is roughly as follows (take the Starter I defined as an example):

Define the XxxProperties class, the property configuration class, and complete the operations related to the property configuration, such as setting the property prefix, which is used to configure in application.properties.

TianProperties Code:

Import org.springframework.boot.context.properties.ConfigurationProperties; @ ConfigurationProperties (prefix = "spring.tian") public class TianProperties {private String name; private int age; private String sex = "M"; / / omit the get set method}

Create the XxxService class to complete the relevant operation logic.

TianService Code:

Public class TianService {private TianProperties properties; public TianService () {} public TianService (TianProperties userProperties) {this.properties = userProperties;} public void sayHello () {System.out.println ("hi, my name is:" + properties.getName () + ", this year" + properties.getAge () + "year +", gender: "+ properties.getSex ());}}

Define the XxxConfigurationProperties class, automatically configure the class, used to complete the Bean creation and other work.

TianServiceAutoConfiguration Code:

@ Configuration @ EnableConfigurationProperties (TianProperties.class) @ ConditionalOnClass (TianService.class) @ ConditionalOnProperty (prefix = "spring.tian", value = "enabled", matchIfMissing = true) public class TianServiceAutoConfiguration {@ Autowired private TianProperties properties; @ Bean @ ConditionalOnMissingBean (TianService.class) public TianService tianService () {return new TianService (properties);}}

Create the directory META-INF under the resources and the spring.factories under the META-INF directory, and load the project's automation configuration class based on this file when SpringBoot starts.

"configuration in spring.factories"

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tian.TianServiceAutoConfiguration

Package the above starter project into a jar package:

Use a custom starter

Create a Spring Boot project test, which looks like the following figure:

Add a custom starter to the pom dependency in the project

Com.tian spring-boot-tian-starter 1.0-SNAPSHOT

TestApplication startup class

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

Configuration in application.properties

Spring.tian.name=tian spring.tian.age=22 spring.tian.sex=M

Write a TestController.java class

RestController @ RequestMapping ("/ my") public class TestController {@ Resource private TianService tianService; @ PostMapping ("/ starter") public Object starter () {tianService.sayHello (); return "ok";}}

After relying on the jar typed by our custom starter

You can see that there is an extra json file.

Finally, start the project and enter

Http://localhost:9091/my/starter

Controller returns ok successfully, and then watch background printing.

Hi, my name is: tian, 22 years old, gender: M

This is a successful reality of the custom starter.

Keywords: out of the box, reduce a large number of configuration items, convention is larger than configuration.

Summary

Spring Boot scans the JAR package that the project depends on at startup, looking for the JAR package that contains the spring.factories file

Then read the spring.factories file to get the configured autoconfiguration class AutoConfiguration`.

Then put the @ Bean that meets the condition (@ ConditionalOnXxx) under the automatic configuration class into the Spring container (Spring Context)

In this way, the user can use it directly to inject, because the class is already in the container.

These are the principles of starter in Spring Boot and how to configure them automatically. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report