Spring Spring Boot: Customizing your Own starter

Source: Internet
Author: User

Concept

In the process of learning spring boot, the most contact is starter. You can think of starter as a service--making it possible for a developer using a feature to not focus on the processing of a variety of dependent libraries, without the need for specific configuration information, by spring boot automatically discovers the required beans through classes under the Classpath path, and weaves the bean

Steps

Introduction of Spring Boot configuration

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

<!--Optional Dependencies--
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<optional>true</optional>
</dependency>

<!--@ConfigurationProperties annotation processing (metadata for IDEs)--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

Add an automatic configuration file

  @org. Springframework.context.annotation.configuration@conditionalonclass ({ Sqlsessionfactory.  class , Sqlsessionfactorybean. class  }) @ConditionalOnBean (DataSource.  class  ) @EnableConfigurationProperties (mybatisproperties.  class  ) @AutoConfigureAfter (datasourceautoconfiguration.  class  )  public   Class   mybatisautoconfiguration {}  
    • Create a new Meta-inf folder under the Src/main/resources directory, and then create a new spring.factories file that tells spring boot to find the specified automatic configuration file, so its contents are
    • # Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=  Org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

Analysis

The regular starter is a standalone project, and then the new warehouse in MAVEN is registered for release, and other developers can use your starter.

The common starter will include the following:

    1. An automatic configuration file that determines whether or not to perform an automatic configuration of the feature, depending on whether the specified class exists for the classpath.
    2. Spring.factories, it is important to instruct spring boot to locate the specified automatic configuration file.
    3. Endpoint: Can be understood as an admin, including the description of the service, interface, interaction (Business information query)
    4. Heath Indicator: Health metrics for the services provided by the starter

During application startup, Spring boot uses the Springfactoriesloader class loader to find the Java configuration file that corresponds to the Org.springframework.boot.autoconfigure.EnableAutoConfiguration keyword. Spring boot iterates through the spring.factories file in the Meta-inf directory of each jar package and builds it into a list of profiles. In addition to the configuration file that corresponds to the enableautoconfiguration keyword, there are other types of configuration files:

    • Org.springframework.context.ApplicationContextInitializer
    • Org.springframework.context.ApplicationListener
    • Org.springframework.boot.SpringApplicationRunListener
    • Org.springframework.boot.env.PropertySourceLoader
    • Org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider
    • Org.springframework.test.contex.TestExecutionListener
Attention
    1. @ConditionalOnMissingBean function is: only the corresponding Bean is not created in the system, its decorated initialization code block will be executed, the user manually created by the bean first ;

    2. How does Spring Boot starter Find automatic configuration files (files such as xxxxautoconfiguration)?

    • Spring.factories: The class in the Classpath directory triggered by Spring boot is detected and configured automatically;
    • @Enable: Sometimes the process of finding an automatic configuration file is triggeredby a user of starter .

Spring Spring Boot: Customizing your Own starter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.