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

Example Analysis of the process of customizing Starter by springboot

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of the springboot custom Starter process, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Customize Starter naming rules

Note the naming rules of artifactId. Spring official Starter is usually named spring-boot-starter- {name} such as spring-boot-starter-web. Spring officially recommends that unofficial Starter naming should follow the format of {name}-spring-boot-starter, such as mybatis-spring-boot-starter. The artifactId of the project created here is helloworld-spring-boot-starter

Develop Starter steps

Create Starter project definition Starter required configuration (Properties) class write automatic configuration class write spring.factories file load automatic configuration class write configuration prompt file spring-configuration-metadata.json (not required)

Specific process

Create a configuration class

@ ConfigurationProperties to define the prefix of the configuration

@ EnableConfigurationProperties (InfluxdbProperties.class) @ ConfigurationProperties (prefix = "spring.influxdb") public class InfluxdbProperties {private String username; public String getDatabase () {return database;} public void setDatabase (String database) {this.database = database;}}

Write automatic configuration classes

@ EnableConfigurationProperties configuration depends on property class @ ConditionalOnProperty configuration Configuration loading rule value refers to which field of Properties havingValue refers to configuring what value value is, and loading Configuration matchIfMissing refers to the default value @ Bean when the field configured by value is not configured. Common conditional dependency annotations peculiar to bean springboot for automatic injection are: @ ConditionalOnBean, this Bean is instantiated only if a Bean exists in the current context. @ ConditionalOnClass, a class is on the classpath before the Bean is instantiated. @ ConditionalOnExpression, which is instantiated only when the expression is true. ConditionalOnMissingBean, which is instantiated only if a bean does not exist in the current context. @ ConditionalOnMissingClass, a class is instantiated only when it does not exist on the classpath. @ ConditionalOnNotWebApplication, this Bean is instantiated only when it is not a web application. @ AutoConfigureAfter, instantiate a bean after it has been automatically configured. AutoConfigureBefore, instantiate a bean before it finishes automatic configuration.

@ Configuration@Order (1) @ EnableConfigurationProperties (InfluxdbProperties.class) @ ConditionalOnClass (InfluxdbProperties.class) @ ConditionalOnProperty (prefix = "spring.influxdb", value = "use-influxdb", havingValue= "true", matchIfMissing = false) public class InfluxdbAutoConfiguration {private String scanEntitySuffix = "Entity.class"; @ Bean@ConditionalOnMissingBean (AiInfluxdbTemplate.class) @ Order (Ordered.HIGHEST_PRECEDENCE) public AiInfluxdbTemplate AiInfluxdbTemplate (InfluxdbProperties influxdbProperties) {return new AiInfluxdbTemplate (influxdbProperties);}}

Write spring.factories files

Spring Boot scans packages at the same level as the startup class by default. If our Starter and startup class are not under the same main package, you need to configure the spring.factories file to take effect.

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ai.base.boot.influxdb.InfluxdbAutoConfiguration

The above is all the contents of the article "sample Analysis of the springboot Custom Starter process". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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

Development

Wechat

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

12
Report