In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to customize starter in Spring Boot2". 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!
WebSockets
Spring Boot provides WebSockets automatic configuration for embedded Tomcat (8 and 7), Jetty 9 and Undertow. If you are deploying the war package to a stand-alone container, Spring Boot will assume that the container is responsible for configuring WebSocket. The Spring framework provides rich WebSocket support by simply adding spring-boot-starter-websocket modules.
Web Services
Spring Boot provides Web Services auto-configuration, and all you need is to define Endpoints. The Spring Web Services feature can be obtained by adding a spring-boot-starter-webservices module.
Create your own auto-configuration
If you are developing a shared libraries in your company, or are developing an open source or commercial library, you may want to develop your own automated configuration (auto-configuration). The autoconfiguration class can be packaged into an external jars and can still be recognized by Spring Boot. Automatic configuration can be associated with a "starter" that provides the code for the auto-configuration and the libraries to be referenced. Let's first explain what you need to know to build your own auto-configuration.
Be careful
Refer to the demo project to learn how to create a starter step by step.
Understand auto-configured beans
At the bottom, auto-configuration is implemented through the standard @ Configuration class. In addition, the @ Conditional annotation is used to constrain the conditions under which automatic configuration takes effect. Usually autoconfiguration classes need to be annotated with @ ConditionalOnClass and @ ConditionalOnMissingBean to ensure that autoconfiguration is applied only if the relevant class is found and no custom @ Configuration is declared. Check out the @ Configuration class (META-INF/spring.factories file) in the spring-boot-autoconfigure source code.
Locate automatic configuration candidates
Spring Boot will check if there is a META-INF/spring.factories file in your published jar. The attribute with EnableAutoConfiguration as key in this file should list your configuration class:
11 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
22 com.mycorp.libx.autoconfigure.LibXAutoConfiguration,\
33 com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
You can use @ AutoConfigureAfter or @ AutoConfigureBefore annotations to specify a specific order for configuration classes. For example, if you provide web-specific configuration, your class needs to be applied after WebMvcAutoConfiguration.
You can also use the @ AutoconfigureOrder annotation to provide sorting for automatic configuration classes that are not aware of each other, which has the same semantics as the regular @ Order annotation, but provides order for automatic configuration classes.
Be careful
Autoconfiguration classes can only be loaded in this way, ensuring that they are defined in a special package and, in particular, cannot be the target of component scans.
Conditional annotation
You almost always need to add one or more @ Conditional annotations to your autoconfiguration class. The @ ConditionalOnMissingBean annotation is a common example that developers can use to override the default behavior provided by the autoconfiguration class.
Spring Boot contains a lot of @ Conditional annotations that you can reuse in your own code by annotating the @ Configuration class or a separate @ Bean method.
Class condition
The @ ConditionalOnClass and @ ConditionalOnMissingClass annotations can determine the inclusion of the configuration based on whether a specific class appears or not. since the annotation metadata is parsed using ASM, you can use the value attribute to refer to the real class, even if the class does not appear under the classpath running the application. You can also use the name attribute if you prefer to use a string as the class name.
Bean condition
The @ ConditionalOnBean and @ ConditionalOnMissingBean annotations can determine the inclusion of bean based on the existence of a particular class. You can use the value attribute to specify beans (by type), or you can use name to define beans (by name). The search attribute is used to limit the ApplicationContext hierarchy to be considered when searching for beans.
Be careful
You need to pay attention to the order in which bean definitions are added, because the calculation of these conditions is based on what is currently being processed. For this reason, we recommend using only @ ConditionalOnBean and @ ConditionalOnMissingBean annotations on autoconfiguration classes (even if you make sure they are loaded after other user-defined beans).
Be careful
@ ConditionalOnBean and @ ConditionalOnMissingBean do not prevent the creation of @ Configuration classes, and using those conditions at the class level is equivalent to tagging each @ Bean method with annotations.
Property condition
The @ ConditionalOnProperty annotation can decide whether to include a configuration based on a Spring Environment attribute, using the prefix and name attributes to specify the configuration to check. By default, any property that exists that is not false will match, and you can also use the havingValue and matchIfMissing attributes to create more advanced detections.
Resource condition
The @ ConditionalOnResource annotation contains the configuration only when a specific resource appears, and you can name the resource using a common Spring convention, such as file:/home/user/test.dat.
Web Application condition
The @ ConditionalOnWebApplication and @ ConditionalOnNotWebApplication annotations can decide whether to include configurations based on whether the application is a 'web application'. A web application is any application that uses Spring WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.
SpEL expression condition
The @ ConditionalOnExpression annotation can decide whether to include the configuration based on the result of the SpEL expression.
Create your own starter
A complete Spring Boot starter may contain the following components:
Autoconfigure module, which contains the code for automatically configuring the class.
The starter module, which provides automatic configuration modules and other useful dependencies, in short, you can start using the library by adding this starter.
Be careful
If you don't need to separate them, you can put automatic configuration code and dependency management into a single module.
Naming
Be sure to provide an appropriate namespace for your starter, do not start with spring-boot, although use a different Maven groupId, we may provide official support for the automatic configuration you are doing in the future.
As a rule of thumb, suppose you are creating a starter for "acme", naming the autoconfiguration module acme-spring-boot-autoconfigure and starter acme-spring-boot-starter, and if there is only one module combining them, you will usually use acme-spring-boot-starter.
In addition, if your starter provides configuration keys, you need to provide an appropriate namespace for them, especially do not use Spring Boot namespaces (e.g., server,management,spring, etc.), these belong to Spring Boot, and we may improve / modify them in the same way in the future, which may destroy your stuff.
Be sure to trigger meta-data generation so that IDE helper can also be used for your keys, and you may want to check the generated META-INF/spring-configuration-metadata.json to make sure that keys is properly documented.
Automatic configuration module
The autoconfiguration module contains everything you need to use the library, and it may also contain a configured keys definition (@ ConfigurationProperties) and a callback interface that defines how the component is initialized.
Be careful
You need to mark the dependency on the library as optional so that it is easier to add the autoconfiguration module to the project. If you do this, the library will not be provided and Spring Boot will fall back to the default settings.
Starter module
The starter module is actually an empty jar, and its purpose is to provide the necessary dependencies needed to use the library. Don't make any assumptions about the project that adds your starter. If the library you are automatically configuring requires another starters, be sure to mention it. Providing an appropriate default dependency set can be difficult, especially if there are a large number of optional dependencies, you should avoid introducing any non-essential dependencies.
This is the end of "how to customize starter in Spring Boot2". 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.