In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to customize Starter in springboot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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 a Starter project
Define the configuration (Properties) classes required by Starter
Write automatic configuration classes
Write spring.factories file to load automatic configuration class
Write a 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 classes
@ ConditionalOnProperty configure the loading rules for Configuration
Which field of Properties does value refer to?
HavingValue refers to loading Configuration when configuring what the value of value is
MatchIfMissing refers to the default value when the field configured by value is not configured.
@ Bean configure auto-injected bean
Common conditional dependency annotations unique to springboot are:
ConditionalOnBean, which 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 "how to customize Starter in springboot" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.