In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge points about how the SpringBoot scene launcher Starters is customized. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.
1. Starters principle 1.1 Starters scene initiator
1. What are the dependencies needed in the scenario?
Such as dependent jar
2. How to write automatic configuration?
Take WebMvcAutoConfiguration auto configuration as an example:
@ Configuration@ConditionalOnWebApplication@ConditionalOnClass ({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class}) @ ConditionalOnMissingBean (WebMvcConfigurationSupport.class) @ AutoConfigureOrder (Ordered.HIGHEST_PRECEDENCE + 10) @ AutoConfigureAfter ({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class}) public class WebMvcAutoConfiguration {public static final String DEFAULT_PREFIX = "; public static final String DEFAULT_SUFFIX ="
@ Configuration specifies that this is a configuration class
@ ConditionalOnXXX automatically configures the class to take effect if the specified condition is established
Automatic assembly sequence
@ AutoConfigureBefore before a specific auto-assembly Class
@ AutoConfigureAfter after a specific autoassembly Class
Specify the order @ AutoConfigureOrder
@ Bean add components to the container
@ ConfigurationPropertie combines the relevant xxxProperties classes to bind the relevant configuration
@ ConfigurationProperties (prefix = "spring.mvc") public class WebMvcProperties {}
@ EnableConfigurationProperties makes xxxProperties effective and adds to the container
Configuration@Import (EnableWebMvcConfiguration.class) @ EnableConfigurationProperties ({WebMvcProperties.class, ResourceProperties.class}) public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {}
Configure the auto-assembly Bean:
The automatic configuration class should be able to load
Load the automatic configuration class that needs to be started, and configure the automatic configuration class marked @ Configuration under META-INF/spring.factories, and the automatic configuration class will take effect.
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
3. Mode
Starter (starter)
The initiator is only used for dependency import
Write a special automatic configuration module
Initiators rely on automatic configuration, others only need to introduce initiators (starters)
Mybatis-spring-boot-starter Custom initiator name-spring-boot-starter
II. Custom Starters
Build the project:
1. Create an empty project first
2. Create two modules: the maven module of initiator starter and the automatic configuration module created by spring initializer.
Initiator maven module
Custom starters
Initializer creation module for spring (create automatic configuration related modules)
Third, code steps
Introduce the coordinate ming-spring-boot-starter-autoconfigurer of configuration class in the pom file of initiator starter
4.0.0 com.ming.springboot ming-spring-boot-starter 1.0-SNAPSHOT com.ming.springboot ming-spring-boot-starter-autoconfigurer 0.0.1-SNAPSHOT
Write a function to say hello.
Package com.ming.springboot;/** * greeting * * / public class HelloService {HelloProperties helloProperties; public HelloProperties getHelloProperties () {return helloProperties;} public void setHelloProperties (HelloProperties helloProperties) {this.helloProperties = helloProperties;} public String sayHello (String name) {return helloProperties.getPrefix () + "-" + name+helloProperties.getSuffix ();}}
HelloProperties and Helloservice for property binding
Package com.ming.springboot;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties (prefix = "com.ming") public class HelloProperties {private String prefix; private String suffix; public String getPrefix () {return prefix;} public void setPrefix (String prefix) {this.prefix = prefix;} public String getSuffix () {return suffix;} public void setSuffix (String suffix) {this.suffix = suffix }}
Automatic configuration class
Package com.ming.springboot;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@ConditionalOnWebApplication / / web application takes effect @ EnableConfigurationProperties (HelloProperties.class) public class HelloServiceAutoConfiguration {@ Autowired HelloProperties helloProperties; @ Bean public HelloService helloService () {HelloService helloService = new HelloService () HelloService.setHelloProperties (helloProperties); return helloService;}}
Then install these two modules into the maven repository
Install the configuration module first because the starter module depends on the configuration module. Someone else can just call our starter module.
Then the initiator starter is also installed in the warehouse so that others can introduce it with coordinates.
Introduce a custom initiator starter in other projects
Com.ming.springboot ming-spring-boot-starter 1.0-SNAPSHOT
Configure application.properties
# Custom initiator startercom.ming.prefix= learns com.ming.suffix= together. Have you paid any tuition fees?
test
@ Autowired HelloService helloService; @ Test public void starterTest () {String sayHello = helloService.sayHello ("Custom starter"); System.out.println (sayHello);} that's all about the article "how to customize the SpringBoot scene launcher Starters". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.
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.